5

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 is my android code:

package com.example.web;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview;
    int i=30;
    @Override
    protected void onCreate(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(i)");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This is my html code:

 <html>
<body>
<div id="texta">text</div>
<script>

function change(num){
document.getElementById("texta").innerHTML=num;
}

</script>

</body>
</html>
lakshman
  • 2,641
  • 6
  • 37
  • 63
user2206969
  • 53
  • 1
  • 1
  • 6

7 Answers7

7

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

webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:change(i)");
    }
});
Tushar
  • 8,019
  • 31
  • 38
  • I think that might be my problem. I will try it now. – user2206969 Mar 28 '13 at 03:33
  • I tried using the function without passing any argument and manually set the value in the javascript section and it worked fine. But when i pass the variable i to the function it doesn't not work. – user2206969 Mar 28 '13 at 03:42
  • This worked for me. Thanks! Tho I think the function will be called everytime the page loads, even for a different URL, say I click on a search result, the function will get called again when the page loads whether it exists or not. To fix that you could use if or switch statements to call the javascript function depending on the url string. – Sujit Oct 03 '20 at 11:52
0

Try:

@Override
protected void onCreate(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)+")");

}
Han Tran
  • 2,073
  • 4
  • 22
  • 37
0

Try it:

String URL = "file:///android_asset/filename.html";
webviewBrowser=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(webview.loadUrl(URL));
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
hRb
  • 61
  • 2
0

Try to change this :

webview.loadUrl("javascript:change(i)");

to this

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

the way you have write it the "i" is not the variable "i" from your java code but just a String named "i".

Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
Christoforos
  • 564
  • 3
  • 13
0

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.

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
0

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

Sanjay Kumar
  • 1,135
  • 14
  • 27
0

enter image description here

 protected void 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() {
   public void 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
       public void onClick(View v) 
       {
         webView.loadUrl("javascript:callFromAndroidActivity
                          (\""+text.getText().toString()+"\")"); }
        });
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dhina k
  • 1,481
  • 18
  • 24