Android WebView, how to handle redirects in app instead of opening a browser

The question:

So right now in my app the URL I’m accessing has a redirect, and when this happens the WebView will open a new browser, instead of staying in my app. Is there a way I can change the settings so the View will redirect to the URL like normal, but stay in my app instead of opening a new browser?


I want the redirecting URL, I just don’t know how to create it, so the only way to get to that URL is through one that will cause a redirect to the one I want.

For example: When you go here: notice how it will redirect the URL to the actual product. In my app, if I open it in a new browser, it will do that just fine, however if I keep it in my app with a WebView, it will show up as though it’s doing a search for k=9780735622777, like this: . OR, it will open the view in the browser and show what is appropriate. However, I want to keep everything in my app.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Create a WebViewClient, and override the shouldOverrideUrlLoading method.

webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        // do your handling codes here, which url is the requested url
        // probably you need to open that url rather than redirect:
        return false; // then it is not handled by default action

Method 2

According to the official documentation, a click on any link in WebView launches an application that handles URLs, which by default is a browser. You need to override the default behavior like this

    myWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;

Method 3

Just adding a default custom WebViewClient will do. This makes the WebView handle any loaded urls itself.

mWebView.setWebViewClient(new WebViewClient());

Method 4

You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.

mWebView.setWebViewClient(new WebViewClient()
            public boolean shouldOverrideUrlLoading(WebView webView, String url)
                return shouldOverrideUrlLoading(url);

            public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
                Uri uri = request.getUrl();
                return shouldOverrideUrlLoading(uri.toString());

            private boolean shouldOverrideUrlLoading(final String url)
                Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);

                // Here put your code

                return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.

Checkout the example code for handling redirect urls and open PDF without download, in webview.

Method 5

Create a class that implements webviewclient and add the following code that allows ovveriding the url string as shown below.
You can see these [example][1]

public class myWebClient extends WebViewClient {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         return true;

On your constructor, create a webview object as shown below.

   web = new WebView(this); web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

Then add the following code to perform loading of urls inside your app

       WebSettings settings=web.getSettings(); 
    web.setWebViewClient(new myWebClient()); 
 web.setWebChromeClient(new WebChromeClient() {

Method 6

Please use the below kotlin code

webview.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {   
                return false 

For more info click here

Method 7

In Kotlin, to navigate within same webView we needed to override the shouldOverrideUrlLoading for webview

If return type is true then navigation will be blocked If return
type is false then navigation will happen

    object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, request:  WebResourceRequest): Boolean {

                    return true
}.also { webView.webViewClient = it }

Method 8

public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    if (url.equals("your url")) {
            Intent intent = new Intent(view.getContext(), TransferAllDoneActivity.class);

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment