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

AndroidAndroid Webview

Android Problem Overview


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?

Edit:

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: http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 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: http://www.amazon.com/gp/aw/s/ref=is_s_?k=k%3D9780735622777&x=0&y=0 . OR, it will open the view in the browser and show what is appropriate. However, I want to keep everything in my app.

Android Solutions


Solution 1 - Android

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:
        view.loadUrl(url);
        return false; // then it is not handled by default action
   }
});

Solution 2 - Android

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() {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return false;
		}
	});

Solution 3 - Android

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

mWebView.setWebViewClient(new WebViewClient());

Solution 4 - Android

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

mWebView.setWebViewClient(new WebViewClient()
		{
			@SuppressWarnings("deprecation")
			@Override
			public boolean shouldOverrideUrlLoading(WebView webView, String url)
			{
				return shouldOverrideUrlLoading(url);
			}

			@TargetApi(Build.VERSION_CODES.N)
			@Override
			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. https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500

Solution 5 - Android

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 {

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(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(); 
    settings.setJavaScriptEnabled(true); 
    
    web.loadUrl("http://www.facebook.com");
    web.setWebViewClient(new myWebClient()); 
   
 web.setWebChromeClient(new WebChromeClient() {
      // 
      //
    }

Solution 6 - Android

Please use the below kotlin code

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

For more info click here

Solution 7 - Android

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 }

Solution 8 - Android

@Override
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);
            startActivity(intent);
        
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - AndroidxandyView Answer on Stackoverflow
Solution 2 - AndroidabbasView Answer on Stackoverflow
Solution 3 - AndroidTillView Answer on Stackoverflow
Solution 4 - AndroidAshish DasView Answer on Stackoverflow
Solution 5 - AndroidDaniel NyamasyoView Answer on Stackoverflow
Solution 6 - AndroidShalu T DView Answer on Stackoverflow
Solution 7 - AndroidRohit SView Answer on Stackoverflow
Solution 8 - AndroidRasul MammadovView Answer on Stackoverflow