Android Confirmation dialog returning true or false Android Confirmation dialog returning true or false android android

Android Confirmation dialog returning true or false


i have post similiar problem in this forum, but finally i get my answer.my problem in that post is how to create separate confirm dialog class who can acces by other class or activity, so with that confirm dialog class we don't need to write long coding.

here's my answer.

First you must create DialogHandler.java

import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import src.app.R;public class DialogHandler {    public Runnable ans_true = null;    public Runnable ans_false = null;    // Dialog. --------------------------------------------------------------    public boolean Confirm(Activity act, String Title, String ConfirmText,            String CancelBtn, String OkBtn, Runnable aProcedure, Runnable bProcedure) {        ans_true = aProcedure;        ans_false= bProcedure;        AlertDialog dialog = new AlertDialog.Builder(act).create();        dialog.setTitle(Title);        dialog.setMessage(ConfirmText);        dialog.setCancelable(false);        dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,                new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int buttonId) {                         ans_true.run();                    }                });        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,                new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int buttonId) {                        ans_false.run();                    }                });        dialog.setIcon(android.R.drawable.ic_dialog_alert);        dialog.show();        return true;    }}

And this is example to call it in another class

public class YourActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        findViewById(R.id.button1).setOnClickListener(myclick);    }    public final Button.OnClickListener myclick = new Button.OnClickListener() {        @Override        public void onClick(View v) {            doclick();        }    };    public void doclick() {        DialogHandler appdialog = new DialogHandler();        appdialog.Confirm(this, "Message title", "Message content",                "Cancel", "OK", aproc(), bproc());    }    public Runnable aproc(){        return new Runnable() {            public void run() {                Log.d("Test", "This from A proc");            }          };    }    public Runnable bproc(){        return new Runnable() {            public void run() {                Log.d("Test", "This from B proc");            }          };    }}


Well, I was going to say that I am very pleased with myself because I found a simple answer, all by myself!
But the truth is that although I find a way to return a value (which I show below). It is of no use.

The real problem is I wanted a synchronous Dialog, a dialog that waits for the user to answer before resuming your code after dialog.show().
There is no such beast in Android. All dialogs are asynchronous, so dialog.show() only posts the dialog in some queue (I think) and continues. Thus you don't get your answer in time.

For all its worth (nothing) below you'll find how to set a value inside the method that builds the dialog. Maybe there are other uses for this technique, not related to the dialog lifecycle.




To give some related info, I'll say that if you replace

boolean answer;

with

final boolean answer;

it is possible to access the variable from within the listener, but it is not possible to assign it a new value, since it was declared as final.

Here comes the trick.
Define the variable as:

final boolean[] answer = new boolean[1];

Some of you already see why this will work. The final variable here is not the single element of the boolean array, is the array itself.
So now you can assign the array element [0] as you wish.

dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {    public void onClick(DialogInterface dialog, int buttonId) {        answer[0] = true;    }});. . .return answer[0];

And finally you can return it from your method.


What you can do is create a Listener for your Alert Dialog that listens to AlertDialogs action using an Interface.

Create an Interface.

public class MyInterface {    DialogReturn dialogReturn;    public interface DialogReturn {        void onDialogCompleted(boolean answer);    }    public void setListener(DialogReturn dialogReturn) {        this.dialogReturn = dialogReturn;    }    public DialogReturn getListener() {        return dialogReturn;    }}

Now, in your class just implement the Interface that you created using implements MyInterface.DialogReturn

then you can set the Listener and get it working as show below,

public class Main extends Activity implements MyInterface.DialogReturn{    MyInterface myInterface;    MyInterface.DialogReturn dialogReturn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                ....        myInterface = new MyInterface();        myInterface.setListener(this);    }   public void Confirm(Context context) {        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) {                myInterface.getListener().onDialogCompleted(true);            }        });        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {            public void onClick(DialogInterface dialog, int buttonId) {                myInterface.getListener().onDialogCompleted(false);            }        });        dialog.setIcon(android.R.drawable.ic_dialog_alert);        dialog.show();         }@Override    public void onDialogCompleted(boolean answer) {        Toast.makeText(Main.this, answer+"", Toast.LENGTH_LONG).show();            if(answer)            // do something            else            // do something    }}