4

After several redirects the url finally ends up in Webform How can i Exit from the webview ? Is There a way i can detect Html element in webview like button in the page. I cannot use Javascript here since i don't own the sites

public class ProductionWebView extends Activity{ private WebView webView; String url ="https://www.google.lk/?gws_rd=ssl"; @SuppressLint("JavascriptInterface") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.production_webview); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setLoadsImagesAutomatically(true); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); webView.loadUrl(url); //ws.setJavaScriptEnabled(true); webView.addJavascriptInterface(new Object() { public void performClick() { // Deal with a click on the OK button } }, "Next"); webView.addJavascriptInterface(new Object() { public void performClick() { } }, "Authorize"); } } 

3 Answers 3

1

You can use a webClient and implement shouldOverrideUrlLoading to intercept all the urls before the WebView loads them.

mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { // Here put your code to exit // return true; //This indicates WebView to NOT load the url; // return false; //Allow WebView to load url } }); 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank You for your Reply. If the web view have several redirects how can you track them with this code? I tried the code but it is not even loading the 1 st url now
@SheshanGamage Did you return false ?
I used this block ` webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { if(webView.getUrl().contains("code")){ return true; } else{ return false; } } }); } ` but its not loading now its giving me false and doesn't load anything
@SheshanGamage You are already getting Url as parameter. Try using that..
The problem is that the method dosent iterate :( it just works for the first time.
1

If you want to manipulate the URL then override this:

public boolean shouldOverrideUrlLoading (WebView view, String url){ view.loadUrl(request.getUrl().toString()); return true; } 

[WebViewClient Reference]

2 Comments

please check link mentioned in my answer@Sheshan Gamage
Check this stackoverflow.com/a/5125620/2826147 it is i think what you needed.@Sheshan Gamage
-1

You need to create a method like this

webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url){ // do your action here view.loadUrl(url); return false; // then it is not handled by default action } }); 

2 Comments

If the web view have several redirects how can you track them with this code? I tried but it is not even loading the 1 st url now
You can put log in shouldOverrideUrlLoading method to get all the redirects info

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.