1

When I display a WebView page in my android device (4.0), I notice that special character such as é è à are not displayed properly, like this enter image description here

and this the code of my WebView.

public class Browser extends Activity{

WebView ourBrow;
String adress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser);

    try{
        getActionBar().setDisplayShowHomeEnabled(false);
    }
    catch (Exception e)
    { }

    try{

        ourBrow = (WebView)findViewById(R.id.wvBrowser);

        ourBrow.getSettings().setJavaScriptEnabled(true); 
        ourBrow.getSettings().setLoadWithOverviewMode(true); 
        ourBrow.getSettings().setUseWideViewPort(true);  
        ourBrow.setWebViewClient(new ourViewClient());  
        ourBrow.setWebChromeClient(new WebChromeClient());
        ourBrow.getSettings().setSupportMultipleWindows(true);



        String url = null;
        Intent intent = getIntent();

        try{
            if(intent != null) {
                if(intent.getExtras() != null) {
                    url = intent.getExtras().getString("url");
                }
            }

            if(url != null && !url.equals("")) {
                ourBrow.loadUrl(url);
            }
        }catch (Exception e){

        }




    }catch(Exception e)
    {

    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if(ourBrow != null) {
        ourBrow.stopLoading();
        ourBrow.onPause(); //pauses background threads, stops playing sound
        ourBrow.pauseTimers(); //pauses the WebViewCore
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_goback, menu);

    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    // action when setting was selected
    case R.id.goback:
        ourBrow.stopLoading();
        ourBrow.onPause(); //pauses background threads, stops playing sound
        ourBrow.pauseTimers(); //pauses the WebViewCore
        ourBrow.destroyDrawingCache(); //removes the view from RAM
        ourBrow = null;
        finish();
        break;

    default:
        break;
    }

    return true;
}

public class ourViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        //view.loadUrl(url);
        view.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
        return true;
    }

}
hvaughan3
  • 10,955
  • 5
  • 56
  • 76
wael
  • 444
  • 4
  • 11

2 Answers2

1

You can change the setting for the page encoding. For example.

WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");

You need to make sure to set these before requesting to load the website.

é should be supported in utf-8 so I don't know why it shouldn't work otherwise.

Andre Yonadam
  • 964
  • 1
  • 13
  • 30
0

Webview has poor support for diacritical marks. In some versions they are not supported at all(as I remember < 4.2) in others they are partially supported. So the most robust way to display them is to scan document for diacritical mark(U+301 as example) and replace them with an image.

s0nicYouth
  • 470
  • 3
  • 15