0

I am beginner in Android development and want to create Android app with JavaScript.

I can run my page in WebView.

I can run JavaScript in WebView.

I can call Android Toast message (or Dailog alert ) from JavaScript to Android.

But I can NOT run JavaScript function from Android !

Can you help me to fix my code

[ Download an android studio project ] http://r7eq.weebly.com

Code of MainActivity.java

package com.example.adnroid_script;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

public class MainActivity extends Activity {
    public static WebView myWebView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView=(WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new jsInterface(), "FromJS");
        myWebView.loadUrl("file:///android_asset/index.html");
    }

    public class jsInterface{
        @JavascriptInterface
        public void ToAndroid(String jsMessage){
            Toast toast = Toast.makeText(getApplicationContext(),"I sent javascript code to myWebView", Toast.LENGTH_LONG);
            toast.show();
            WebView myWebView = (WebView) findViewById(R.id.webview);
            //   ### The bug is here ###   //
            myWebView.loadUrl("javascript:document.write('<div>Android say : It is working now !</div>');");
        }
    }
}
Adnan
  • 21
  • 4
  • 1
    possible duplicate of [How does evaluateJavascript work?](http://stackoverflow.com/questions/19788294/how-does-evaluatejavascript-work) – ben75 May 24 '15 at 19:18
  • Thanks .. but it's another subject ! I think that (evaluateJavascript) NOT working in old Android systems (Before KitKat) And it's NOT working in my codes (on KitKat also) !!! – Adnan May 26 '15 at 11:33

0 Answers0