Reputation: 13
I want to make a confirm function in java just like a alert in javascript
.
Example javascript code:
var a = prompt("");
In java i have a function that returns a string with a dialog box input;
public static boolean log ;
public String value;
public String androidPrompt(){
log = true;
showMyDialog();
while(log){
}
return value;
}
public void showMyDialog(){
log= false;
value = //inputed value from dialog;
}
But my application don't respond. What should i do. I want to pause
androidPrompt()
while showMyDialog()
is not done. and when showMyDialog()
is done androidPrompt()
function will resume and return the value
Upvotes: 0
Views: 1361
Reputation: 505
The question is a bit unclear about where the call is being invoked. If you want to execute Java (android) functions in JavaScript, then you must create an Interface.
Here is an example of emulating an alert() function using an Interface.
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
//at the oncreate of your activity where webviews is being created
WebView myWebView = findViewById(R.id.sidmyWebView);
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); //this name will be used in JS
myWebView.getSettings().setAllowFileAccess(true);
myWebView.clearCache(true); //temporary for css reload
WebSettings myWebSettings = myWebView.getSettings();
myWebSettings.setJavaScriptEnabled(true); //this enables js on this page
//now, in JS file execution, you can emulate the js alert function as
//alert("Show toast"); //this will not show anything, as you noted
Android.showToast("Calling Alert-show in Android WebView - Java/JavaScript"); //this will work
But if your requirement is simply the normal alerts/dialogues, then read on acceptable non-obstructive methods allowed by Android.
Upvotes: 0
Reputation: 1007474
Example javascript code: var a = prompt("");
Android does not support that sort of blocking UI call. Instead, use listeners to notify something within your app when the user has accepted the dialog (e.g., pressed the OK button).
Upvotes: 0