Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Confirmation dialog returning true or false
    primarykey
    data
    text
    <p>It seems to be there is no easy way to get an Alert dialog to return a simple value.<br> This code <strong>does not work</strong> (the <em>answer</em> variable cannot be set from within the listener, in fact it does not even compile)</p> <pre class="lang-js prettyprint-override"><code>public static boolean Confirm(Context context) { boolean answer; AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.setTitle("Confirmation"); dialog.setMessage("Choose Yes or No"); dialog.setCancelable(false); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int buttonId) { answer = true; } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int buttonId) { answer = false; } }); dialog.setIcon(android.R.drawable.ic_dialog_alert); dialog.show(); return answer; } </code></pre> <p>NOTE: It is important that the method is self contained, i.e., it does not depend on variables or constructs external to it. Just call it and get your answer, true or false.</p> <p>So, what to do? This simple wish of returning <strong>true</strong> or <strong>false</strong> seems to be much more complicated than it deserves.</p> <p>Also, the setButton method has the form:</p> <pre class="lang-js prettyprint-override"><code>dialog.setButton(int buttonId, String buttonText, Message msg) </code></pre> <p>But it is not clear how to use it, where is the meesage sent to, to whom, which handler is used?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload