How to open activity by Android WebView hyperlink click or How to handle hyperlink click in Android WebView
Case 1:
I have Android WebView with some HTML content. I need to open activity when user click on some hyperlink.
Case 2:
I have Android WebView with some HTML content. I need to do something when user click some hyperlink.
HOW TO DO THIS.
Below you can download source code of this sample Android project
1. To do this your WebView must have your custom WebViewClient
2. Your Android WebView HTML content must have specific hyperlinks.
Lets see source code of Activity:
package com.hrupin.sample.webviewintent;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SendIntentByHyperlinkClickActivity extends Activity {
private WebView webView1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView1 = (WebView) findViewById(R.id.webView1);
String summary = "<html><head><title>Title of the document</title></head><body><h1><a href=\"hrupin://second_activity\">LINK to second activity</a></h1><h1><a href=\"http://www.google.com/\">Link to GOOGLE.COM</a></h1></body></html>";
webView1.loadData(summary, "text/html", null);
webView1.setWebViewClient(new MyWebViewClient(this));
}
}
The sourcecode of MyWebViewClient must be something like this:
Pay attantion in shouldOverrideUrlLoading method.
package com.hrupin.sample.webviewintent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
private Context context;
public MyWebViewClient(Context context) {
this.context = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("hrupin://second_activity")) {
Intent i = new Intent(context, SecondActivity.class);
context.startActivity(i);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
That’s all.
Here you can download source code of sample project to see sample of usage.
8 Comments
Usman · 28 August, 2012 at 13:52
Nice work thanks !
Isuru Madusanka · 26 December, 2013 at 09:44
Nice work lot of use full to me 🙂
Kym · 3 June, 2014 at 06:00
Nice work! Thanks ^^
çorum çilingir · 21 October, 2017 at 18:46
thank you
Shanavas Rahiman · 5 November, 2017 at 10:00
How to download a file in android studio webview when i click the a href link?
Shanavas Rahiman · 5 November, 2017 at 10:06
How to download a file when i click the a href link in android studio web view link
Subhanjan · 26 December, 2017 at 06:49
hello thanks for the tutorial can you tell me how to get this url in the second activity and load the webview?
Dharam Pal Jindal · 11 June, 2019 at 05:45
It shows an Error: Cannot Resolve symbol: MyWebViewClient