0

I'm trying to call JS functions from Android native code, but they don't seem to be working. I've tried many solutions but to no solution. Here is my code:

public class MainActivity extends Activity {

private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyCustomWebViewClient());

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);

webView.loadUrl("http://google.com");

}

private class MyCustomWebViewClient extends WebViewClient {

@Override
public void onPageFinished(WebView view, String url) {
    webView.loadUrl("javascript:alert('shadow');");
    Toast.makeText(getApplicationContext(), "DONE", Toast.LENGTH_LONG).show();

}
} 


} 

Please help.

Thanks

1 Answer 1

1

You can't do alert() with Android's WebView. If you want to show alert dialogs, you need to handle it in your Activity code or use a WebChromeClient. Per the docs:

Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks).

See: https://developer.android.com/reference/android/webkit/WebView.html

With a WebView you can still write a JavaScript function that sets the value of a textbox to some string. If you need specific code to help you with this, let me know. If you've gotten this far though, you probably can handle it on your own is my guess.

Edit 1

First create a method in JavaScript called sendValueToAndroid()

myWebView.loadUrl("javascript:sendValueToAndroid()");   

In the JavaScript method, call an exposed method in your Android code.

function sendValueToAndroid()
{
    val divValue = ...
    Android.sendValueToAndroid(divValue);
}

Expose a method in the Android app in any object of your choosing.

@JavascriptInterface
public String sendValueToAndroid(String val)
{
    //do something in your app
}

Basically, what you're doing is telling the WebView to invoke a JavaScript method which invokes a callback method in your own app.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply. Using alert, I was just checking to see if it works or not. My main goal is to pull out value of a 'div' from a web-page. I can call JS to do that i.e. "javascript:document.getElementsByClassName('myName')[0].innerHTML;" . But still figuring out how can I get value from it. Its like calling a function which "returns" some value. This is still a mystery to me as of now. May be you can help here to speed up my task :)
Sure. Give me a minute. I had to do this exact thing in a project I was working on.
@shadow Check the edit to my answer. If you need more help, please let me know.
Thank you for your effort :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.