Michael Stoner
Michael Stoner

Reputation: 1401

Android WebView not rendering webpage correctly

I have a strange problem with my webview. I have a basic webview that I use to display webpages. This works the majority of the time, but there is one site in particular that does not render. I am not sure how to begin to debug this problem. Here is my initialization code for the webview:

    webView = (WebView) findViewById(R.id.rss_webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {

            pb.setProgress(progress);


            if (progress == 100)
                pb.setVisibility(View.GONE);
            else
                pb.setVisibility(View.VISIBLE);

        }


    });

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);

    webView.loadUrl(URL);

The URL that is having problems is: http://twitter.com/AdamSchefter/statuses/236553635608285184

This page (and other twitter pages) render as a blank gray square on the screen with no content.

I have noticed that if I open the url with my normal Android browser it gets redirected to a mobile version. The webview does not do this as far as I can tell. When setting breakpoints in the shouldOverrideUrlLoading() method, I don't receive any redirect urls.

I have scanned logcat for any indication of error. I don't think the webview performs any logging to log cat at all (other than a not that a webview was loaded) because there is nothing there.

I am not sure how to track down this issue. Any thoughts?

I have a lot of twitter sources that I need to display and I don't have control over the URL.

Upvotes: 2

Views: 6200

Answers (1)

Michael
Michael

Reputation: 3332

Ok done by adding below line in my code

webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U;`
Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like
Gecko) Version/4.0 Mobile Safari/530.17");

I got it from this post at stackoverflow Problems loading mobile.twitter in webview.

android mobile twitter page in webview not opening?

Also, if it's not redirecting you to the mobile version of twitter, you can just append mobile to it, i.e. mobile.twitter.com

Upvotes: 3

Related Questions