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>

7 Answers 7

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)");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I think that might be my problem. I will try it now.
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.
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.
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)+")");

}

7 Comments

Please flesh out your answer.
Edited answer, check it :)
That you didn't try very hard.
Explain WHY you suggest the answer you're suggesting.
You don't see any change from the code at the question and the my answer or NOT? A programer can easy see the different and WHY of this answer. Any other problem?
|
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.

Comments

0

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

Comments

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()+"\")"); }
        });

Comments

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));

Comments

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.