4

I am developing an android app, which has a webview in it..

The URL is https://www.facebook.com/ when the post is clicked in the webview the post URL is not getting in should overidding url but webview is redirecting the facebook to that particular post what is the problem the url should be in the shouldoverriding method of the webviewclient

Here I am Setting the Custom WebView Client 

webView.setWebViewClient(new WebViewController());

public class WebViewController extends WebViewClient {
@Override
        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            Log.e("urltop", str + " ==top");
            String str3 = "youtube";
            if (!str.contains("market://")) {
                if (!str.contains("mailto:") && !str.contains("play.google") && !str.contains("tel:") && !str.contains("intent:") && !str.contains("vid:") && !str.contains(str3) && !str.contains("fb-messenger://")) {
                    if (!str.contains("whatsapp://")) {
                        if (str.contains("m.me")) {
                            Log.e("urlm.me", str);
                            return true;
                        } else if (str.contains("jpg")) {
                            Log.e("urljpg", str);
                            return true;
                        }
                        return true;
                    } else {
                        Log.e("url", str);
                    }
                }
            }
            return true;
        }

Thanks in advance :)

Heet Parkhiya
  • 117
  • 1
  • 11
pranavsethi96
  • 421
  • 2
  • 6
  • 12

2 Answers2

7

Just using shouldOverrideUrlLoading:

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("YOURLINK")) {
            Intent intent = new Intent(getContext(), YourActivity.class);
            startActivity(intent);
            return true; // Handle By application itself
        } else {
            view.loadUrl(url);
            return true;
        }
    }
}
edwoollard
  • 12,245
  • 6
  • 43
  • 74
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • Thank You... It helped me.. :) .... I just had to edit getContext() to MainActivity.this and the rest worked! – pranavsethi96 Jul 22 '13 at 13:42
  • 1
    Just change the return type from false to true when u want to take the control. From doc: `If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url` – AlexAndro Nov 10 '14 at 22:18
0

For that you need to check and use WebViewClient.

By using WebViewClient, you can monior any action user makes with WebView. Now, as you want to detect URL click and want to start new activity based on it, check: shouldOverrideUrlLoading.

Check WebView page for the WebViewClient example.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295