Isaac
Isaac

Reputation: 2364

Android: Mobile Site works in Chrome but not WebView

I'm working on a website (sorry I can't post it), and a mobile app for it. The mobile app is actually a part of the website, and we use Java WebView to access it. The styling for the mobile app is fine when I access it from any mobile browser (Chrome, Safari, and even Android's default browser), but everything is bigger when I use the mobile app. Everything is essentially styled to look the way you would get it if you tried accessing the mobile page from a desktop browser. Here is the code for initializing the web view:

private void initWebView() {
    if (mWebView == null) {
        mWebView = new WebView(this);
        mWebView.setWebViewClient(new CustomWebViewClient());
        mWebView.setWebChromeClient(new CustomWebChromeClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    //setContentView(R.layout.activity_main);
    setContentView(mWebView);
}

From my research, the most common recommendations/suggestions were to make sure that JavaScript is enabled (which it is), and to set the user agent string. I tried setting the user agent string to several things (all failed) before testing what the default user agent string was. When I don't set a user agent string, the default is Mozilla/5.0 (Linux; Android 5.1; Android SDK built for x86 Build/LKY45) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36. That is the same exact user agent string that the Android's default browser uses, and the styling for the mobile site works fine there.

I also replaced the CustomWebViewClient with the regular WebViewClient and the CustomWebChromeClient with the regular WebChromeClient and got the same results. So the customizations have no effect on this.

I also changed the home URL for the mobile app to https://www.google.com to see if it was even capable of loading the mobile version of a website, and it succeeded.

So I've ruled out JS, the user agent string, the customizations we've made, and after checking Google I'm thinking it may not even be the mobile app, but something on the website. Now I am stuck. I don't know what to do anymore.

What could be causing our mobile app (using Java WebView) to load Google's mobile site appropriately but not our site (which loads fine on all other mobile browsers)?

Upvotes: 2

Views: 4394

Answers (1)

Isaac
Isaac

Reputation: 2364

Added this:

mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);

Upvotes: 2

Related Questions