How To Detect Div Tag From Html File?
I am loading html page in web view. In html there is a button; on click of that, I have to start a new activity. I have displayed html file but how to detect the click of button ?
Solution 1:
use this :
Html:
<divid="button"onclick="Helper.methodeone()"><ul><li><atitle="Forming a Habit"href="Forming_Habit.html">
More
<spanclass="arrows"> »</span></a></li></ul>
on java :
webView.setWebViewClient(newMyWebViewClient());
WebSettingswebSetting= webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
//webSetting.setBuiltInZoomControls(true);
webSetting.setAllowContentAccess(true);
web.addJavascriptInterface(newSamlplejavaconect(), "Helper");//NEW LINE
webSetting.setDefaultTextEncodingName("utf-8");
webView.loadUrl("file:///android_asset/"+htmlFile);
And in below class you can manage activity :
publicclassSamlplejavaconect {
publicvoidmethodeone() {
Toast.makeText(this, "Hi", Toast.LENGTH_LONG).show();//FOR EXAMPLE
}
}
Solution 2:
Try this
MainActivity.java
publicclassMainActivityextendsActivity {
WebView webView;
JavaScriptInterface jsInterface;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
WebSettingswebSettings= webView.getSettings();
// enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(newWebChromeClient() {
});
jsInterface = newJavaScriptInterface(MainActivity.this, webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "AndroidFunction");
webView.loadUrl("file:///android_asset/book.html");
}
publicclassJavaScriptInterface {
private Activity activity;
WebView wb;
publicJavaScriptInterface(Activity activiy, WebView wb) {
this.activity = activiy;
this.wb = wb;
}
@JavascriptInterfacepublicvoidactivity() {
try {
Classactivity= Class.forName("com.example.classname");
IntentmyIntent=newIntent(getApplicationContext(), activity);
startActivity(myIntent);
finish();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
book.html
<!DOCTYPE html><html><body><buttontype="button"onclick="startactivity()">Click Me!</button><script>functionstartactivity() {
AndroidFunction.activity();
}
</script></body></html>
Post a Comment for "How To Detect Div Tag From Html File?"