0

I searched Google and stack exchange and found about 15 different solutions, none of which work. They either throw syntax errors upon compiling, or they just don't work on the website I'm requesting. (Tradingview.com/chart)

When I go to my target website in chrome and click Request desktop, it works fine. In the webview in the app I'm building, it doesn't work. Google displays as desktop, but my target site does not. However, it does display properly in chrome or Firefox, just not my app.

What I tried:

Every solution provided here: Setting WebView to view Desktop Site and Not Mobile Site

Including this library https://github.com/delight-im/Android-AdvancedWebView/blob/master/README.md

This How to open desktop site in webview in android

That How to load Desktop view instead of mobile view in webview

The other setUserAgentString in Android webview has no effect on HTTP header used in loadURL()

Most of those don't cause any change (on my target site), while a few of them throw syntax errors, sometimes up to 13 of them.

I tend to see comments being left saying something to the effect of, "Contact the website owner to make them change their site." That's not an acceptable solution, and is unhelpful without further explanation. How is it that in chrome, Firefox, Opera, etc. I'm well able to see a desktop version of the page, but it isn't working on the webview? This needs to be explained if the only answer is that it's"the website's fault contact the owner."

Right now I've got this in here,

WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setUserAgentString("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/57.0");
    webView.loadUrl("http://facebook.com");

This works fine on Facebook.com for example, but not TradingView.

Matt Zabojnik
  • 179
  • 12

1 Answers1

0

After much trial and error, I came to a solution. This isn't perfect, but much closer. (Maybe comments can help correct my code where I've fallen short)

webview_chart.getSettings().setLoadWithOverviewMode(true);

webview_chart.setInitialScale(200);
 webview_chart.getSettings().setUseWideViewPort(false);
 webview_chart.getSettings().setMinimumFontSize(16);

String newUA= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19";
 webview_chart.getSettings().setUserAgentString(newUA);
 webview_chart.loadUrl("http://tradingview.com/chart");

What this seems to do is spoof a larger screen size, which causes responsive design to send desktop version rather than mobile. It's not perfect, as you can see I have to adjust minimum font size up so things are readable, and on smaller screens it still doesn't work.

But that's the reason you can send desktop headers and still get a mobile site.

(I haven't found an explanation like this anywhere else, so I think it can be helpful to others having this same problem)

Matt Zabojnik
  • 179
  • 12
  • Will you able to see your desktop site with the above code? – Vignesh May 28 '18 at 06:17
  • @ASV, only somewhat. It had lots of problems doing it that way, like terrible font size, etc. I ended up hiring a freelancer who implemented a javascript library that gets it to render well on all screen sizes, with appropriate font size and everything. – Matt Zabojnik May 29 '18 at 15:09
  • Oh no, i was about to ask you the fix – Vignesh May 29 '18 at 15:13
  • If you have time please check this once https://stackoverflow.com/questions/50578112/whatsapp-web-redirecting-issue?noredirect=1#comment88174256_50578112 – Vignesh May 29 '18 at 15:13