0

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

2
  • You might want to have a look at this Commented Jul 10, 2013 at 12:21
  • If your function call is from WebView, then you must create a JavaScript Interface; if not, then just read about accepted prompts / dialogs as allowed by Android. Also, all JS codes may not respond unless the interface is used. Commented Nov 13, 2023 at 9:43

2 Answers 2

0

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).

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.