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()\"
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.
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));
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)+")");
}
try webview.loadUrl("javascript:change(\""+i"\")");
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)");
}
});
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()+"\")"); }
});