Reputation: 49
I have an HTML file with button, The button will popup an alert message when we click on to the button.
I placed the HTML file assets folder, Its correctly loading to the emulator and i can see the button but the issue i am facing is, the button click is not responsive, I cant see the popup message.
I also enabled the JavaScript but the result was negative. Then I changed the alert event to the JavaScript code but not worked. Here I am adding my code, I appreciate if anybody can help.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(com.example.test.R.layout.activity_main);
myWebView= (WebView) findViewById(com.example.test.R.id.webView1);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/myown.html");
}
The following is my HTML page code
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="alert('Hello world!')">Click Me!</button>
</body>
</html>
Upvotes: 0
Views: 2160
Reputation: 33534
The below code is from my production environment and is completely working, I have stored the html
file into my assest
folder and then accessing it into an Activity
.
Code:
String uri = "file:///android_asset/htmlpages/" + index + ".htm";
WebView wb = (WebView) findViewById(R.id.webView_Glossary);
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl(uri);
Upvotes: -1
Reputation: 1090
Edit> This might not be the simplest solution, but it does offer extra functionality
When I created something similar, it worked on 4.1.2. The alert gave a nice, holo styled popup. I used an online resource, and a webchromeclient (see answer before me).
However, if you want to be sure this works, you might want to take a look at Building Web Apps in Webview page from Android Developers. It explains the JavaScriptInterface. You can use that to couple javascript from your HTML page (local, or online) but have it execute native Java code instead.
EG You can use HTML like this
<input type="button" value="Click Me!" onClick="showAndroidToast('Hello World!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
Add this class
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
After that, add this code to the sample you provided.
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
Upvotes: 1
Reputation: 20553
You need to use a webChromeClient
and override onJsAlert()
function for this purpose:
WebView wv=new WebView(this);
wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Do what you need to after alert is received
return super.onJsAlert(view, url, message, result);
}
Upvotes: 2