Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web View onclick event for a specific link in that webview

I have a webview and I am calling data in that wv from a webservice, and in that whole description in a webview, there is a link in the last. so, my problem is that i want to open a new activity onclick of that link only neither onclick of webview nor ontouch of webview

like image 407
Java_Android Avatar asked Jul 25 '13 15:07

Java_Android


1 Answers

You need to provide an implementation for shouldOverrideUrlLoading. You'll have to setup a WebViewClient for your webview and inside of this method, you'll need to have some logic that recognized that link and then open the new Activity. Something like:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wv = (WebView) findViewById(R.id.myWebView);
        wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(isURLMatching(url)) {
                    openNextActivity();
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    protected boolean isURLMatching(String url) {
            // some logic to match the URL would be safe to have here
        return true;
    }

    protected void openNextActivity() {
        Intent intent = new Intent(this, MyNextActivity.class);
        startActivity(intent);
    }
like image 56
gunar Avatar answered Oct 03 '22 11:10

gunar