4

WebView is not showing website correctly. Any help would be great! The code ive used work on all another site. Not sure whats the issue. Any thing I should add? Works well in chrome and other browsers so don't know what to do. Any help would be great!

WebView

enter image description here

Chrome

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);

        }
    }
}
user2407147
  • 1,508
  • 2
  • 22
  • 40

2 Answers2

5

A few suggestions:

Move loadUrl() after configuring your WebView (I noticed that in the comments, but it should be down there regardless).

It looks like the styles are missing from your page. Either they failed to load, or are somehow being disabled by the WebView. Try adding

myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); and myWebView.getSettings().setDomStorageEnabled(true);

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

And FYI, you're not actually using MyWebViewClient for anything, and instead relying on the default WebViewClient.

ggmathur
  • 833
  • 5
  • 12
  • Thanks for the comments. I've done all the above but it ends up just a zoomed version of of before. But thanks for all suggestions. – user2407147 Jul 14 '13 at 19:34
  • Bummer. I tried it out on my own WebView, which loads other styles correctly. Since this is a Japanese site, perhaps there are some characters the WebView is having trouble parsing? Are there any non english characters in the CSS URLs? – ggmathur Jul 14 '13 at 19:58
  • I'm not 100% as i do not own the site. But it is a Japanese website. – user2407147 Jul 14 '13 at 21:33
  • I've tried loading the desktop site but comes out funny with some images no in frame, but really wanted the mobile site to show :S – user2407147 Jul 14 '13 at 21:56
0

I don't know if this fixes the problem, but when you call loadUrl on a WebView the WebViewClient methods are invoked on a background thread. I don't think you can call UI stuff directly from a background thread. See How to start an activity from a thread class in android? for an example.

Community
  • 1
  • 1
  • The thing is that the code works on every other site I've tried. But thanks – user2407147 Jul 14 '13 at 21:53
  • I believe that the behaviour if you call UI stuff from off the main thread is undefined (random). Not sure, though. Did you try running all the UI stuff from on the main thread? –  Jul 14 '13 at 21:57
  • Not sure how I would implement that, could you show using my code. – user2407147 Jul 16 '13 at 10:10