2

I need to open desktop site in android webview for that i have tried as below but it is not working.

String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45

5 Answers5

8

This is the perfect solution:

 private static final String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
 private static final String MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; U; Android 4.4; en-us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";

 //Choose Mobile/Desktop client.
 WebSettings settings = mWebView.getSettings();
 settings.setUserAgentString(DESKTOP_USER_AGENT);

UPDATE

The above MOBILE_USER_AGENT is no longer supported. so use the given below user agent

private static final String MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 9; Redmi 6 Pro Build/PKQ1.180917.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.105 Mobile Safari/537.36";
Abx
  • 2,852
  • 4
  • 30
  • 50
vishal patel
  • 94
  • 1
  • 3
2

Try this:

    webView = (WebView)findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
1

You can use setDesktopMode(true) from this WebView subclass or read how it's implemented. What is does is (a) set the user-agent not to include the words "mobile" or "Android" and (b) set the viewport to a larger width.

saurabh gupta
  • 491
  • 6
  • 18
1

For Mobile User-Agent

webview.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 4.4; en-us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");

For Desktop site Agent

browser.getSettings().setUserAgentString("Mozilla/5.0 (Linux; diordnA 7.1.1; suxeN 6 Build/N6F26U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 eliboM Safari/537.36");

A lot of searches this code fine working on Android webview.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
0

This is working for me: refer to this as mentioned by @saurabh gutpa as well

public void setDesktopMode(final boolean enabled) {
    final WebSettings webSettings = webview.getSettings();

    final String newUserAgent;

    if (enabled) {
        newUserAgent = webSettings.getUserAgentString().replace("Mobile", "eliboM").replace("Android", "diordnA");
    }
    else {
        newUserAgent = webSettings.getUserAgentString().replace("eliboM", "Mobile").replace("diordnA", "Android");
    }

    webSettings.setUserAgentString(newUserAgent);
    webSettings.setUseWideViewPort(enabled);
    webSettings.setLoadWithOverviewMode(enabled);
    webSettings.setSupportZoom(enabled);
    webSettings.setBuiltInZoomControls(enabled);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sajad Bin Nazir
  • 313
  • 3
  • 12