Search code examples
androidandroid-webviewandroid-viewpagerwebviewclient

Loading Facebook in a WebView, behaves weird


I'm loading m.facebook.com in my WebView, not sure how important that is, but the webview is in a container within a ViewPager.

Every time I launch the application and load the url I see one of the following results:

One which you would actually expect:

enter image description here

But the other is quite disturbing:

enter image description here

Why are these two behaviors?

The XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/Site_WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

The setup code for the WebView:

webView.loadUrl(item.getUrl());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUserAgentString("My User Agent - Android");
webView.setWebChromeClient(new WebChromeClient() {});
webView.setWebViewClient(new WebViewClient() {});

I inflate the xml in the adapter, and add it to the container...


Solution

  • After understanding this is not a race condition between the ViewPager, and the drawer, I've dug into the page, t find out that Facebook customize the page width according to some parameters which I cared not about...

    Later I've installed user agent switcher extension to Chrome, and been able to reproduce it, with a my custom user agent.

    I did not expect the user agent to have such an effect on a page...

    But hell, I've been wrong a billion times before!