Please try this at once:
Activity code:
public class MainActivity extends AppCompatActivity {
private WebView mWebView = null;
private ProgressBar mLoading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mWebView = (WebView)findViewById(R.id.wvPortal);
mLoading = (ProgressBar) findViewById(R.id.pbLoading);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setSupportMultipleWindows(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
/* To keep page navigation within the WebView and hence within the app,
we need to create a subclass of WebViewClient,and override its shouldOverrideUrlLoading(WebView webView, String url) method.*/
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.loadUrl("file:///android_asset/www/index.html");
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
Log.d("Test 0","Interface calls");
//calling javascript interface:
// 1st.parameter is the JavaScript interface object itself.
// 2nd.parameter is the name of the global JavaScript variable which the JavaScript interface object is bound to.
mWebView.addJavascriptInterface(new AppJavaScriptProxy(this,mWebView), "androidAppProxy");
}
}
AppJavaScriptProxy class for creationg interface :
public class AppJavaScriptProxy {
private Activity activity = null;
private WebView webView = null;
public AppJavaScriptProxy(Activity activity,WebView webview) {
this.activity = activity;
this.webView = webview;
Log.d("Test 1","in Interface");
}
@JavascriptInterface
public void showMessage(final String message) {
Log.d("from javascript", "" + message);
final Activity theActivity = this.activity;
final WebView theWebView = this.webView;
this.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!theWebView.getUrl().startsWith("file:///android_asset/www/index.html")) {
return;
}
Toast toast = Toast.makeText(
theActivity.getApplicationContext(),
message,
Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
Index.html call this script in html page.
<input type="button" value="call Methode" onClick="showMessage("Message from JavaScript")" />
<script>
if(typeof androidAppProxy !== "undefined"){
androidAppProxy.showMessage("Message from JavaScript");
} else {
alert("Running outside Android app");
}
</script>
call this script on any button click on webview index page.
Ref. Building webapps in webview
If above code snippet would helps you please vote my answer. Thanks!