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.










