android user agent

AndroidUser Agent

Android Problem Overview


I am writing an app in Android that uses a WebView to display HTML content. I was told to get an Android user agent for my app - how do I do that? I opened http://whatsmyuseragent.com from my app as well as the Android browser - both the user agents are the same.

Please help!

Android Solutions


Solution 1 - Android

After much research, I figured it out. There is a way to set a user agent for Android WebView.

webview.getSettings().setUserAgentString("user-agent-string");

http://developer.android.com/reference/android/webkit/WebSettings.html

Solution 2 - Android

Put this in the onCreate method of the java class for the activity that displays the WebView:

WebView myWebView = (WebView)findViewById(R.id.webview);
//get the UA of the current running device:
String userAgent = view.getSettings().getUserAgentString() ;
//set the UA of the webview to this value:
myWebView.getSettings().setUserAgentString(userAgent);

Don't use System.getProperty("http.agent") - this will return the 'Dalvik' user agent (Dalvik is the VM that individual Android apps run within)

Solution 3 - Android

You can't currently set the user-agent for WebView.

Update - I stand corrected!

In WebSettings there is a method called setUserAgentString:

webView.getSettings().setUserAgentString("my-user-agent");

Solution 4 - Android

You can use System.getProperty("http.agent") to get the default device UA. And the webView.getSettings().getUserAgentString(); will give you the UA of the WebView. Be aware that we can set the UA programmatically. So it might not be the default device UA in all the cases.

System.getProperty("http.agent") is the better way to get the UA and can be retrieved before an instance of WebView is available.

Solution 5 - Android

If you would like to experiment with a custom User-Agent in the standalone Browser application (not an embedded WebView inside an application), you can manipulate the User-Agent value by typing "about:useragent" in your browser's URL field (without the quotes ""), and then load the page.

You will see a Dialog with radio buttons to simulate Iphone, Desktop, Lismore, Nexus One, Galaxy S or even a Custom User Agent edit box.

After you select/edit as per your needs, tap OK and you're set.

Cheers!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSagar HatekarView Question on Stackoverflow
Solution 1 - AndroidSagar HatekarView Answer on Stackoverflow
Solution 2 - AndroidChris HalcrowView Answer on Stackoverflow
Solution 3 - AndroidMatthew WillisView Answer on Stackoverflow
Solution 4 - AndroidSripathiView Answer on Stackoverflow
Solution 5 - AndroidAndrei MărcuţView Answer on Stackoverflow