Skip to content Skip to sidebar Skip to footer

Calling Javascript Function From An Android Activity

I wan to call a javascript function from an android activity but it doesn't seem to work. I have used the android webview function webview.loadUrl('javascript:function()'); This i

Solution 1:

It's likely your page isn't fully loaded by the time you're trying to execute the javascript. Can you try:

webView.setWebViewClient(newWebViewClient() {

   publicvoidonPageFinished(WebView view, String url) {
        view.loadUrl("javascript:change(i)");
    }
});

Solution 2:

Try:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = (WebView)findViewById(R.id.webView1);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/index.html");
    webview.loadUrl("javascript:change("+String.valueOf(i)+")");

}

Solution 3:

What is "i", If "i" is a number then Specify the Number, if "i" is a character then kindly enclose it in double Quotes, you are calling a method of some arguments, so kindly correct your mistake.

Solution 4:

try webview.loadUrl("javascript:change(\""+i"\")");

Solution 5:

enter image description here

protectedvoid onCreate(Bundle savedInstanceState) 
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   final Activity MyActivity = this;
   text=(EditText)findViewById(R.id.textValue);
   Show=(Button)findViewById(R.id.textButton);
   webView= (WebView) findViewById(R.id.webView);
   getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                                    Window.PROGRESS_VISIBILITY_ON);
   webView.setWebChromeClient(new WebChromeClient() {
   publicvoid onProgressChanged(WebView view, int progress) {

   MyActivity .setTitle("Loading...");
     MyActivity .setProgress(progress * 100);

     if (progress == 100)
     MyActivity .setTitle("Android Dhina");
    }
   });
     webView.setWebViewClient(new WebViewClient());
     webView.addJavascriptInterface(new WebAppInterface(this), "Android");

     webView.getSettings().setJavaScriptEnabled(true);
     webView.loadUrl("file:///android_asset/web.html");

     Show.setOnClickListener(new OnClickListener()
      {
       @Override
       publicvoid onClick(View v) 
       {
         webView.loadUrl("javascript:callFromAndroidActivity
                          (\""+text.getText().toString()+"\")"); }
        });

Post a Comment for "Calling Javascript Function From An Android Activity"