Reputation: 3685
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
Upvotes: 6
Views: 13018
Reputation: 196
The only solution which worked for me (javascript will be executed many times, but this is the only working solution for now)
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));", null);
}
You can set desktop UA string too
webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
Upvotes: 1
Reputation: 31497
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.
Upvotes: 3
Reputation: 3147
You Can try this
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0");
Upvotes: 1