Android WebView how to handle redirects in an application, not open a browser

So, right now, in my application, the URL that I am accessing is redirected, and when that happens, WebView will open a new browser instead of staying in my application. Is there a way to change the settings so that View redirects the URL as usual, but remains in my application and does not open a new browser?

Edit:

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

For example: When you go here: http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 pay attention to how to redirect the URL to the actual product. In my application, if I open it in a new browser, it will be fine, but if I save it in my application using WebView, it will appear as if it is searching k = 9780735622777, for example: 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 suitable. However, I want to save everything in my application.

+102
android android-webview
Nov 01 '10 at 3:13
source share
6 answers

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 } }); 
+226
Nov 01 '10 at 3:34
source share

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

  myWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }); 
+16
Sep 20 '13 at 16:47
source share

Just add a custom default WebViewClient . This forces WebView to handle any loaded URLs.

 mWebView.setWebViewClient(new WebViewClient()); 
+12
Nov 25 '15 at 20:18
source share

You will need to set your own WebviewClient override method shouldOverrideUrlLoading 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. } }); 

Check out the sample code to handle redirect and open PDF URLs without downloading in a webview. https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500

+7
Feb 24 '17 at 7:09
source share

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

 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 load the URLs inside your application.

  WebSettings settings=web.getSettings(); settings.setJavaScriptEnabled(true); web.loadUrl("http://www.facebook.com"); web.setWebViewClient(new myWebClient()); web.setWebChromeClient(new WebChromeClient() { // // } 

Download demo

+2
Jul 15 '16 at 9:11
source share

Please use the Kotlin code below

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

For more information click here.

0
May 09 '19 at 8:53
source share



All Articles