Webview Tricks and Tips

To enable back button should be not close the application and web pages navigation only,

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
               webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


Webview settings:

        WebSettings webSetting = sWebview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDisplayZoomControls(false);
        //Set whether the DOM storage API is enabled.
        webSetting.setDomStorageEnabled(true);
        sWebview.setWebChromeClient(new WebChromeClient());
        sWebview.setWebViewClient(new MyWebViewClient());

To interact between Java and Javascript:

WebAppInterface.Java

import android.app.Activity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

public class WebAppInterface {
    Activity activity;
    ResultInterface resultInterface;
    WebView webView;

    /** Instantiate the interface and set the context */
    WebAppInterface(Activity activity, ResultInterface resultInterface, WebView webView) {
        this.activity = activity;
        this.resultInterface = resultInterface;
        this.webView = webView;
    }


    public void displayMessage(String message){
       final String url = "javascript:displayJavaMsg('"+message+"')";
        if(!activity.isFinishing()){
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl(url);
                }
            });
        }
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void passValue(String value) {
        resultInterface.result(value);
        Toast.makeText(mContext, value, Toast.LENGTH_SHORT).show();
    }
}

ResultInterface.Java

public interface ResultInterface {
    void result(String value);
}


In your MainActivity.Java

WebAppInterface webInterface;

In OnCreate:

sWebview.loadUrl("file:///android_asset/index.html")
 webInterface = new WebAppInterface(MainActivity.this, new ResultInterface() {
            @Override
            public void result(String result) {
                Log.i("web_result", result);
            }

        }, sWebview);

 sWebview.addJavascriptInterface(webInterface, "Android");

 webInterface.displayMessage("Java to Javascript");

 In your html, index.html

 <!doctype html>
<html>
<head>
    <title>Android WebView Example</title>
</head>
<body>
<script type="text/javascript" src="script.js"></script>
<input type="text" id="txt_amt" style="padding:5px; margin: 20px;">
<div style="margin: 10px;">
    <button id="btn_show" type="button" onclick="printAll()" style="background-color: #008CBA;border: none;color: white;padding: 15px 32px;
    text-align: center;text-decoration: none;display: inline-block;font-size: 16px;">Click here
    </button>
</div>
</body>
</html>


 script.js

 function printResult(){
Android.passValue("Javascript to Java")
}

 function displayJavaMsg(msg){
alert(msg);
}






Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter