Android Sent Text To JavaScript

While developing Android application, sometimes we need to use webview and want our android (java code) communicate with html in webview. We can use javascript to communicate between java to html and vice versa. Dont forget to setting javascript enabled in our webview to true.

Please check code below to check how Android sent text to javascript :

public class MainActivity extends AppCompatActivity
    public class MyJavaScriptInterface {
        Context mContext;
        MyJavaScriptInterface(Context c) {
            mContext = c;
        public void showToast(String toast){
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        public void openAndroidDialog(String msg){
            AlertDialog.Builder myDialog
                    = new AlertDialog.Builder(MainActivity.this);
            myDialog.setTitle("Text From JavaScript");
            myDialog.setPositiveButton("OK", null);

    WebView webView;
    EditText editText;
    protected void onCreate(Bundle savedInstanceState)

        //webview initialization
        webView = (WebView)findViewById(;
        final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
        webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");


        //sent data from android to javascript
        editText = (EditText)findViewById(;
        Button button = (Button)findViewById(;
        button.setOnClickListener(new Button.OnClickListener()

            public void onClick(View arg0)
                // TODO Auto-generated method stub
                String msgToSend = editText.getText().toString();
                webView.loadUrl("javascript:callFromActivity(\"" + msgToSend + "\")");


Create file index.html code in assets directory. This is the contents from this html Android sent text to javascript file

    <script type="text/javascript" src="myscript.js"></script>

<body style="background-color:powderblue;">
Text From Android :
<input id="txtBoxImport" readonly>
Sent Text To Android :
<input id="txtBoxExport">
<input name="btnSent" type="button" value="Sent" onClick="sent_to_android()">

Dont forget to create javascript file with name myscript.js and save to assets directory. This javascript is the important part, becuase used to communicate between java android and javascript (webview).

function sent_to_android() {
    msg = document.getElementById("txtBoxExport").value;

function callFromActivity(msg){
    document.getElementById("txtBoxImport").value = msg;

Please check below image for output Android sent text to javascript code :

Image. Sent Text From Android To JavaScript
Sent Text From Android To JavaScript
Image. Sent Text From JavaScript Android
Sent Text From JavaScript Android

This is a simple tutorial how Android sent text to javascript and how to javascript or webview sent text to Android. I hope this tutorial can help you solve your problem. You can download the compete code Android sent text to javascript in here.

Source :

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *