Pdee As-Diddy
Pdee As-Diddy

Reputation: 157

How to load Desktop view instead of mobile view in webview

Am designing an android web app, when i run on the emulator the desktop version shows but when i run on my android device mobile view rather loads. What i want is to load the desktop view on my device

Upvotes: 0

Views: 4160

Answers (1)

UnTraDe
UnTraDe

Reputation: 3867

The server uses the User-Agent header in the HTTP request to determine if the device is mobile or desktop. You can request the website with modified User-Agent header so that the server will think it's desktop device.

Andorid code:

Map<String, String> headers = new Map<String, String>();
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
loadUrl("www.google.com",  headers);

For more info: http://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String, java.util.Map)

http://en.wikipedia.org/wiki/Mobile_device_detection

And for a list of available User-Agent strings: http://www.useragentstring.com/Firefox25.0_id_19710.php

Edit:

After looking again at the WebView documentation, I found this in the description of the loadUrl function:

Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView's defaults.

Which means, you cant override User-Agent in the way I wrote above. After searching a bit on the internet I found this: setUserAgentString in Android webview has no effect on HTTP header used in loadURL()

And came up with this code:

webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
webview.loadUrl("www.google.co.il");

Please try it and check if that solves your problem.

Upvotes: 2

Related Questions