Search code examples
androidwebviewandroid-studiowebviewclientwebchromeclient

Do something if WebView url contains a string


I have a WebView in which I load a webpage. In this webpage the user needs to do some actions. A specific action needs to be executed when the user arrives on a specific webpage. This action should execute automatically.

How can I continuously check the current URL in the WebView and connect an action to a certain condition?

An example code is:

String url = wv.getUrl();
if (url.contains("string"))
{
    code = url.substring(url.lastIndexOf("value=") + 6);
    dialog.dismiss(); //action
}

However, where can I place this so it continuously checks the URL of the WebView? Any help is appreciated.


Solution

  • You have,

     webView.setWebViewClient(new WebViewClient()
      {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {               
    
            String url = webView.getUrl();
            if (url.contains("string"))
            {
               code = url.substring(url.lastIndexOf("value=") + 6);
               dialog.dismiss(); //action
            }
            return false;                            
        }
     }
    

    Ref : http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,java.lang.String)