Translate this page

How to Set Custom User Agent in Android WebView

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




Related Posts

Get Your Own Android Mobile App for Your Website Today

Are you looking to expand your online presence and reach more customers? In today’s digital age, having a mobile app is an essential tool to connect with…

Why making an app for your website can help your business to grow

In today’s digital age, it’s more important than ever to have a strong online presence. Whether you’re running a business, a blog, or a personal website, having…

person using macbook pro on person s lap

Fix android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference

Here is how to Fix Android Studio Error Attempt to invoke virtual method ‘boolean android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference This error is caused by a number…

Android Spinner onItemSelected Listener Example

Spinner is an android UI element; it provides a drop-down List where you can choose an item from the list. You will need a listener to catch…

person in gray shirt holding a small paper with texts

Get Current Month Name in Java Android

The code below can be used to get the name of current month, for example, January, February, March etc. Just call this method and it will return…

The future of android developers is not good

Are you an android developer or looking to be an android developer? You should read this Myself is zid, I’m an android developer and been in this…

This Post Has 4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *