WebView does not display correctly

WebView does not display the website correctly. Any help would be great! The ive code used works on all other sites. Not sure what the problem is. Any thing I should add? It works well in Chrome and other browsers, so I donโ€™t know what to do. Any help would be great!

Webview

enter image description here

Chromium

enter image description here

 public class Website extends Activity { WebView myWebView; LinearLayout root; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.website); myWebView = (WebView) findViewById(R.id.webView); root = (LinearLayout) findViewById(R.id.root); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.addJavascriptInterface(new WebAppInterface(this), "Android"); myWebView.setWebViewClient(new WebViewClient()); myWebView.getSettings().setBuiltInZoomControls(true); myWebView.getSettings().setSupportZoom(true); myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); myWebView.getSettings().setDomStorageEnabled(true); myWebView.loadUrl("http://dspart.org/"); getActionBar().setDisplayHomeAsUpEnabled(true); } public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavascriptInterface public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("http://dspart.org")) { // This is my web site, so do not override; let my WebView // load the page return false; } // Otherwise, the link is not for a page on my site, so launch // another Activity that handles URLs Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (myWebView.canGoBack()) { myWebView.goBack(); } else { root.removeView(myWebView); myWebView.removeAllViews(); myWebView.destroy(); this.finish(); } return true; } } return super.onKeyDown(keyCode, event); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); root.removeView(myWebView); myWebView.removeAllViews(); myWebView.destroy(); this.finish(); return true; default: return super.onOptionsItemSelected(item); } } } 
+4
source share
2 answers

A few suggestions :

Move loadUrl() after setting up your WebView (I noticed that in the comments, but it should be there there independently).

It looks like styles are missing from your page. Either they did not load, or they were somehow disabled using WebView . Try to add

myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); as well as myWebView.getSettings().setDomStorageEnabled(true);

It also looks like the WebView is enlarged:
Try removing myWebView.getSettings().setUseWideViewPort(true); myWebView.getSettings().setLoadWithOverviewMode(true); myWebView.getSettings().setUseWideViewPort(true); myWebView.getSettings().setLoadWithOverviewMode(true);

And FYI, you are not actually using MyWebViewClient for anything and instead rely on the default WebViewClient .

+5
source

I do not know if this problem fixes, but when you call loadUrl in WebView, the methods of WebViewClient are called in the background thread. I donโ€™t think you can call UI stuff directly from the background thread. See How to run an event from a thread class in android? for example.

0
source

Source: https://habr.com/ru/post/1491334/


All Articles