65

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!

Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72

5 Answers5

157

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

Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
  • 2
    And you can test it if you load this url: https://www.whoishostingthis.com/tools/user-agent/ Just in case :D – Ultimo_m May 16 '18 at 14:58
  • @Ultimo_m, thank you! I noticed it worked in API 21 and 29, but user-agent didn't change in API 30 emulator. – CoolMind Jun 17 '20 at 16:54
  • 1
    Thanks! See https://stackoverflow.com/questions/62434410/set-user-agent-in-webview-for-android-api-30/62535531. It won't work in API 30. – CoolMind Jun 23 '20 at 13:11
17

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)

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
13

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");
Matthew
  • 44,826
  • 10
  • 98
  • 87
6

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.

arserbin3
  • 6,010
  • 8
  • 36
  • 52
Sripathi
  • 1,760
  • 15
  • 20
  • The default UA of the webview is the system UA. But the UA of the webview can be set manually(programmatically). So it may give you the different UA than the default one if you changed the UA of your webview. So its sager to get the system UA. – Sripathi Apr 18 '14 at 14:33
0

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!

Andrei Mărcuţ
  • 1,273
  • 17
  • 28