Search code examples
androidandroid-studiowebviewandroid-webview

Android WebView Set Custom UA With Android OS Version


I want to set Custom User Agent for my Android WebView App with App Version & Android OS.

I am using the following function for doing so:

myWebView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 5.1.1; FixMatka v1.0 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36");

I want to set the users' device's Android Version in place of Android 5.1.1

Also, if possible users' device's Chrome Version in place of Chrome/43.0.2357.65

& users' device's Build Version in place of Build/LMY48B;

How do I do it? Please explain step by step. I am relatively new in Android Development.


Solution

  • Try this:

    String OSVersion = android.os.Build.VERSION.RELEASE;
    
    myWebView.getSettings().setUserAgentString(String.format("Mozilla/5.0 (Linux; Android %s; FixMatka v1.0 Build/IMM76B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/72.0.3626.96 Mobile Safari/537.36", OSVersion));