i have JS which return the value
function sendDisplay()
{
var dis = alert(reader.dom.find('controls_contents_container').style.display);
return dis;
}
in android i try to set the return value so i can make a if else statement in android
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
CookieManager.setAcceptFileSchemeCookies(true);
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.cover);
super.loadUrl("file:///android_asset/www/index.html",1500);
}
@Override
public void onBackPressed() {
super.loadUrl("javascript:alert(sendDisplay())");
}
}
when i alert in loadUrl method, its show value from JS. but i dont know how to set that value to android variable.
TQ
Take a look at Google’s guide section on Binding Javascript Code to Android Code. I’ll also post a short summary here:
You’ll first need to set up a Java class:
Then, you’ll need to add an instance of that class to your WebView as a javascript interface:
Finally, you can call it using “Android.showToast(whatever)” from javascript, or you can call that javascript from your Java code:
Which is a little redundant in the case of showing a Toast message. But hopefully you get the idea.