31

I've done quite a lot of research on Stack Overflow and a lot of Google research but nothing I find is actually working out for me. I want the site to view the desktop site instead of the mobile site. How do I do this? I want it to directly go to the Desktop site.

WebView myWebView = (WebView) findViewById(R.id.webview); 
    myWebView.loadUrl("http://www.apotter96.webs.com/");
}
user2039764
  • 361
  • 1
  • 3
  • 8
  • Don't think its really a possibility, since its out of your hands what their site does when it notices you're a mobile. Maybe if you find out how they recognize and save it, you can manipulate it with some javascript or whatever – Stefan de Bruijn Feb 04 '13 at 13:44
  • How am I supposed to use javascript to do that? – user2039764 Feb 04 '13 at 13:46
  • Sounds like you're looking for some method to change the user's Agent String? Have you searched around for that it seems there are quite a few resources when I google it. – Grambot Feb 04 '13 at 14:02
  • 2
    You can use `setDesktopMode(true)` from [this library](https://github.com/delight-im/Android-AdvancedWebView) or read how it's [implemented](https://github.com/delight-im/Android-AdvancedWebView/blob/6db5a76b781c636513282b9efed993e705db4570/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java#L358). – caw Mar 30 '16 at 17:32
  • setting myWebView.setInitialScale(100); worked for me, but before myWebView.loadUrl() add webView.setWebViewClient(new WebViewClient()); – Kifayat Ullah Jun 15 '19 at 22:19
  • https://stackoverflow.com/q/73670361/19900349 – Gopal Lal Sep 21 '22 at 06:40

12 Answers12

45

Change the user agent of webview

 String newUA="Foo/"; // Change this to desired UA

like

 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);
baboo
  • 1,983
  • 17
  • 23
  • 1
    How do I enable Flash Player? – user2039764 Feb 04 '13 at 14:00
  • try this http://stackoverflow.com/questions/6596243/problem-to-load-flv-video-in-webview/6855609#6855609 – baboo Feb 04 '13 at 14:02
  • 2
    This is working fine, but I got a sideeffect because of using this setting. The webview is not destroyed in onDestroy() of the activity. I am using mWebView.destroy(); in the onDestroy funtion of the activity – user2041902 Jul 05 '13 at 11:43
  • @BhanuSharma I have posted my answer can check that and let me know – Vignesh Jun 13 '19 at 05:24
  • watch for user agent string , it is now slightly modified and also include chrome version. that might effect the end result ... look at developer's official : https://developer.android.com/guide/webapps/migrating#UserAgent – Muahmmad Tayyib Nov 25 '20 at 13:17
22

This method helps you to set DesktopMode on webview

public void setDesktopMode(WebView webView,boolean enabled) {
    String newUserAgent = webView.getSettings().getUserAgentString();
    if (enabled) {
        try {
            String ua = webView.getSettings().getUserAgentString();
            String androidOSString = webView.getSettings().getUserAgentString().substring(ua.indexOf("("), ua.indexOf(")") + 1);
            newUserAgent = webView.getSettings().getUserAgentString().replace(androidOSString, "(X11; Linux x86_64)");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        newUserAgent = null;
    }

    webView.getSettings().setUserAgentString(newUserAgent);
    webView.getSettings().setUseWideViewPort(enabled);
    webView.getSettings().setLoadWithOverviewMode(enabled);
    webView.reload();
}

Call it like that

Mobile mode : setDesktopMode(webView, false);

Desktop mode : setDesktopMode(webView, true);

For Kotlin:

fun setDesktopMode(webView: WebView, enabled: Boolean) {
    var newUserAgent: String? = webView.settings.userAgentString
    if (enabled) {
        try {
            val ua: String = webView.settings.userAgentString
            val androidOSString: String = webView.settings.userAgentString.substring(
                ua.indexOf("("),
                ua.indexOf(")") + 1
            )
            newUserAgent = webView.settings.userAgentString.replace(androidOSString, "(X11; Linux x86_64)")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } else {
        newUserAgent = null
    }
    webView.settings.apply {
        userAgentString = newUserAgent
        useWideViewPort = enabled
        loadWithOverviewMode = enabled
    }
    webView.reload()
}
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34
18

You can use WebView to show view as Desktop Site with fit in mobile display.

        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);
bastami82
  • 5,955
  • 7
  • 33
  • 44
Abhijeet Sharma
  • 189
  • 1
  • 2
6

Some sites don't use User Agent to determine if then have to show the mobile or the desktop version of the Page. Some pages uses screen size to do this.

I build an app to use a page in desktop mode, but it doesn't work properly. Always show the mobile version because the page uses screen size and not User Agent String.

TunebaGamer
  • 61
  • 1
  • 2
6

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) {
           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");
Vlad
  • 7,997
  • 3
  • 56
  • 43
Sali Manaf
  • 166
  • 1
  • 9
  • Everything else in here is simply about the user agent which is not enough for many web sites. That one works for me too, thanks for sharing. That should be the accepted answer until something better comes along. – Slion Jun 14 '20 at 14:56
  • I tried a few things to find away to have the initial scale zoomed out to fit the screen but none I was happy with. Looks like I'm going to leave it to 1.0 and let user zoom out for now. – Slion Jun 14 '20 at 15:32
  • 2
    Found it: view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (window.screen.width / 1024));", null) – Slion Jun 14 '20 at 15:38
  • You also need to make sure wide viewport is enabled: webView.getSettings().setUseWideViewPort(true); – Slion Jun 14 '20 at 17:25
  • 2
    @Slion saved my night !! :P – Muahmmad Tayyib Nov 25 '20 at 16:06
  • Actually I'm pretty sure you can do JavaScript injection from `onPageStarted` as it seems it is called after the main HTML document has been loaded. Thus it won't be called multiple times. – Slion Sep 29 '22 at 16:33
1

A little update to accepted answer.This is the new string. Wrote this because someone had an issue of "Update Browser" in the comments.

String newUA= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
mWebView.getSettings().setUserAgentString(newUA);
J.Doe
  • 23
  • 8
1

If you have update browser error you can try this to set apple safari UA or replace UA with Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 This worked 100% for me.

webview =(WebView)findViewById(R.id.webView);
        webview.getSettings().setMinimumFontSize(12);
        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);
        String newUA= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50";
        webview.getSettings().setUserAgentString(newUA);
        webview.loadUrl("https://solveforum.com");
0

You need to change the user agent : http://developer.android.com/reference/android/webkit/WebSettings.html#setUserAgentString(java.lang.String)

Here is an example :
Loading html data in WebView

Community
  • 1
  • 1
EvZ
  • 11,889
  • 4
  • 38
  • 76
0

After long search this worked for me -

    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);
A J
  • 4,542
  • 5
  • 50
  • 80
0

Try with this

String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
Vignesh
  • 592
  • 6
  • 25
0

This worked for me

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);

    webView.loadUrl("https://web.whatsapp.com/");

    String userAgent = webView.getSettings().getUserAgentString();

    try {
        String androidString = webView.getSettings().getUserAgentString().
                substring(userAgent.indexOf("("),userAgent.indexOf(")")+ 1);

        userAgent = webView.getSettings().getUserAgentString().replace(androidString,"X11; Linux x86_64");

    }catch (Exception e){
        e.printStackTrace();
    }

    webView.getSettings().setUserAgentString(userAgent);
    webView.reload();
Neeleshwar Kumar
  • 335
  • 3
  • 13
0

The easiest way is in Java:

- Mobile / Phone Mode

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setUseWideViewPort(false);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.setInitialScale(110);
  • Desktop Mode

      webView.getSettings().setJavaScriptEnabled(true);
    
      webView.getSettings().setUseWideViewPort(true);
    
      webView.getSettings().setLoadWithOverviewMode(true);
    
Mori
  • 2,653
  • 18
  • 24