-2

I am translating a platform from C# to android.

And I am stuck on how to wait for the Dialog response, to determine which action will be done. (It will always bypass the decision.)

I have tried to use handler, but it doesn't work.

Any help is appreciated.

Here are my code, which alertDialog wont wait for Dialog box Response finish.

public boolean CheckWriteCriteria()
{

        new AlertDialog.Builder(Labelling.this)
                .setTitle("Test 1")
                .setMessage("Question_1")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
                    }
                }).show();


        new AlertDialog.Builder(Labelling.this)
                .setMessage("Question_2")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
                    }
                }).show();


    return true;
}




 public void Testing()
{

    if(CheckWriteCriteria())
    {
        Toast.makeText(this, "Testing is before AlertDialog.", Toast.LENGTH_SHORT).show();
    }

}

1 Answers1

1

Android support AlertDialog. Let see

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

    // set title
    alertDialogBuilder.setTitle("AlertDialog Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Some Alert Dialog message.")
            .setCancelable(false)
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                            Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show();

                }
            })
            .setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                           Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show();

                    dialog.cancel();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

only Ok and Cancel action here, if you want more, just define custom dialog.

kemdo
  • 1,429
  • 3
  • 15
  • 29
  • I have tired this solution, however, the code below alertDialog wont wait for Dialog box Response finish. – cealan2015 Mar 19 '18 at 04:21
  • FYI, when you press OK -> dialog disappear then "OK button click " show in screen, same with CANCEL button. Please give more detail information if it still not working for you – kemdo Mar 19 '18 at 04:26
  • I have uploaded the detail information that i would like to do,Is it possible to Wait for Dialog box Response? – cealan2015 Mar 19 '18 at 12:36