Shishdem
Shishdem

Reputation: 498

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.

Upvotes: 1

Views: 6642

Answers (1)

Bharath Mg
Bharath Mg

Reputation: 1127

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)

Upvotes: 5

Related Questions