Set Custom User Agent Android WebView
Updated on: June 29, 2021
In webview of Android, if you want to set custom user agent,this tutorial is for you.
Custom user agent can be helpful if some options of a website is only available in the desktop version of the site – not on mobile version, or you just want your app to have the option to change user agent, etc
To set a custom user agent, we need define the user agent in a string, then pass the string to setUserAgentString(your string name here).
Create a new string
In the activity of the webview, create a new string,
String MyUA = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
So we created a string for user agent, the user agent defined in the string is of a desktop browser user agent, with this user agent set – your Android browser will load desktop version of websites. You can change this user agent to other user agents you like.
So we set the string, now let’s call it
webview = (WebView) findViewById(R.id.web1); webview.getSettings().setUserAgentString(MyUA);
Here we called the string that we have set, it is ready now.
This code can be inside the oncreate method of the web activity.
Meet Advanced Plus
I highly recommend you use the new and advanced Android Webview Advanced Plus Source Code we developed to easily convert any website to android app. No coding required, just set your website link, app color, icon etc and the app will be ready!, and it supports upload, download, loading progress bars, notification, night mode etc. To learn more about the Android Advanced Webview Source Code and to download it, head over to this page Download Android Webview Source Code
Thanks! Its working
glad it worked!
Hi.
It works, but some pages still showing the mobile version.
What i’m doing wrong?
Thanks.
I am getting an error. Cannot resolve getsettings()
Can you please help