Call javascript function from android activity

I want to call a javascript function from android activity, but it does not work. I 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> 
+4
source share
7 answers

Your page is probably not fully loaded by the time you try to execute javascript. Can you try:

 webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:change(i)"); } }); 
+5
source

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)+")"); } 
0
source

Try:

 String URL = "file:///android_asset/filename.html"; webviewBrowser=(WebView)findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.setWebViewClient(new WebViewClient(webview.loadUrl(URL)); 
0
source

Try changing this:

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

to that

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

as you write it, "i" is not a variable "i" from your java code, but simply a string named "i".

0
source

What is β€œi”, if β€œi” is a number, then indicate β€œNumber”, if β€œi” is a character, then voluntarily enclose it in double quotation marks, you call the method of some arguments, so please kindly correct your mistake.

0
source

try webview.loadUrl ("JavaScript: change (\" "+ I" \ ")");

0
source

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

All Articles