Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force webview to display desktop sites

I have the below webview client which sets the user agent to a desktop browser when we are viewing a page that does not contain the word google in the URL. (Also does other stuff but that all works fine).

 webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                     if (!url.contains("google")) {
                        String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
                        webView.getSettings().setUserAgentString(newUA);
                         view.loadUrl(url);  
                     }else {
                            webView.getSettings().setUserAgentString(null);
                            view.loadUrl(url);  
                     }
                    return true;
                }
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    super.onPageStarted(view, url, favicon);
                    progressBar.setVisibility(View.VISIBLE);
                }
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    String page = webView.getUrl();
                    if (!(page.contains("google"))){
                        grabit.setVisibility(View.VISIBLE);

                    }else{
                        grabit.setVisibility(View.GONE);

                    }
                    webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('"+CFG.Bookmarklet+"');");
                    progressBar.setVisibility(View.INVISIBLE);
                    if (webView.canGoBack()){
                        left.setImageResource(R.drawable.ic_arrowleft);
                    }else{
                        left.setImageResource(R.drawable.ic_arrowleft_gray);
                    }
                    if (webView.canGoForward()){
                        right.setImageResource(R.drawable.ic_arrowright);
                    }else{
                        right.setImageResource(R.drawable.ic_arrowright_gray);
                    }

                }
            });

This issue with this is that while on some sites it works perfectly others it does not work and on some it seems to just change the view port. A few examples are:

> Argos - shows mobile
> Tesco - shows mobile but view port has changed 
> Amazon - works 
> John Lewis - shows mobile but view port has changes
> Play.com - works

So is there something I am missing? Another way the websites it does not work on are checking the browser to decide what to display?

It would seem that the 'show desktop version' in Chrome works fine for these sites.. so prehaps chrome does something else to?

Thanks

like image 934
Zac Powell Avatar asked Nov 13 '13 12:11

Zac Powell


1 Answers

You can use setDesktopMode(true) from this WebView subclass or read how it's implemented in detail.

This way, you don't have to set a fixed user-agent string. Moreover, setting the user-agent string only is usually not enough. It depends on the site, of course, since every website uses their own way of determining whether the client is on mobile or desktop.

like image 143
caw Avatar answered Sep 18 '22 12:09

caw