user1808920
user1808920

Reputation: 181

Display accented characters in webview

My app does not display accented characters correctly. I looked into this last year but was not able to find a solution. I've been digging around but still have not found a solution. I've supplied a screen shot that shows what happens when the user enters "London Château" in an EditText field which is loaded into a WebView when the button is clicked. The accented character "â" does not translate as expected. It translates to "Ãç", "London ChÃçteau". Would love to get a solution, Thanks in advance.

I have supplied code to demonstrate:

public class MainActivity extends Activity {

private WebView webView;
private EditText edittext_1;
private Button buttonUpdate;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webview_summary);
    webView.getSettings().setJavaScriptEnabled(true);

    edittext_1 = (EditText) findViewById(R.id.editText_1);
    buttonUpdate = (Button) findViewById(R.id.button_update);

    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String s = edittext_1.getText().toString();
            webView.loadData(s, "text/html", "utf-8");
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webview_summary"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight=".85" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight=".15" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/editText_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:hint="text"
            android:minWidth="100dp"
            android:text="" />

        <Button
            android:id="@+id/button_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/editText_1"
            android:padding="10dp" />
    </RelativeLayout>
</LinearLayout>

</LinearLayout>

Upvotes: 3

Views: 2010

Answers (2)

user1808920
user1808920

Reputation: 181

As soon as I posted I found a suggestion that worked. I changed the following line from:

webView.loadData(s, "text/html", "utf-8");

to:

webView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);

Upvotes: 15

yital9
yital9

Reputation: 6712

try to get s from Html.fromHtml

String s = Html.fromHtml(edittext_1.getText().toString()).toString();

Upvotes: 0

Related Questions