Open Android Activity On Html Button Click In Web View
I have an Android program to display an HTML page in a web view. The HTML page exists locally in 'asset/www/index.html'. I want to put a button on the HTML page and open a new acti
Solution 1:
You have to pass url on button click from html like below,
index.html
<html><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width; user-scalable=0;" /><scripttype="text/javascript"charset="utf-8"src="jquery-2.0.0.min.js"></script><scripttype="text/javascript"charset="utf-8"src="quantize.js"></script><title>My HTML</title></head><body><h1>My HTML</h1><INPUTTYPE="button"value="Test"onClick="window.location='Navigation://OpenNativeScreen'"></body></html>
Now you will get the that url in shouldOverrideUrlLoading method of web view when button click.see the below code,
MainActivity.java
publicclassMainActivityextendsActivity {
WebView myBrowser;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropdown_html);
myBrowser = (WebView) findViewById(R.id.mybrowser);
myBrowser.setWebViewClient(newMyBrowser());
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.loadUrl("file:///android_asset/www/index.html");
}
privateclassMyBrowserextendsWebViewClient {
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("Navigation://OpenNativeScreen")) {
startActivity(newIntent(MainActivity.this,SecondActivity.class));
finish();
returntrue;
}
returnfalse;
}
}
}
Post a Comment for "Open Android Activity On Html Button Click In Web View"