user8810567
user8810567

Reputation:

Show alert and move to next activity

I have included alertdialog in my activity.

If I click Yes, it should move to next activity, and if I click No, its should move to another.

But the dialog is not even appearing when I do. Here is my code. Is it possible to do like that?Can anyone help. But if I call just finish() when I click no, its working.

Here is the code :

            final Button submitCustomer = (Button) findViewById(R.id.submit_customer);



        submitCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                confirmRemoveOrder();

            }
        });




            private void confirmRemoveOrder() {

        new AlertDialog.Builder(this)
                .setTitle("Confirm")
                .setMessage("Are you sure you want to cancel this order ?")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        deleteOrder();
                    }
                })
                .setNegativeButton("No",  new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        createCustomer();
                    }
                });



    }







}

Upvotes: 1

Views: 216

Answers (4)

ashwanth s
ashwanth s

Reputation: 115

You just add .show() for your alertDialog

new AlertDialog.Builder(this)
            .setTitle("Confirm")
            .setMessage("Are you sure you want to cancel this order ?")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    deleteOrder();
                }
            })
            .setNegativeButton("No",  new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    createCustomer();
                }
            }).show();

Just adding it will fix your problem.

Upvotes: 0

Abhishek kumar
Abhishek kumar

Reputation: 4445

Create your AlertDialog by using below code:

You forget to add:

AlertDialog alert = dialog.create();
alert.show();

Create Dialog Full Code:

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Confirm");
dialog.setMessage("Are you sure you want to cancel this order ?" );

dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //Action for "Yes".
        deleteOrder();
    }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
        //Action for "No".
        createCustomer();
     }
 });

final AlertDialog alert = dialog.create();
alert.show();

Don't forget to dismiss dialog before going to another Activity.

Reference : AlertDialog Documentation

Upvotes: 2

AskNilesh
AskNilesh

Reputation: 69699

You just fortgot to .show(); method of AlertDialog.Builder() to display your AlertDialog.Builder()

change it like below

CODE

private void confirmRemoveOrder() {

        new AlertDialog.Builder(this)
                .setTitle("Confirm")
                .setMessage("Are you sure you want to cancel this order ?")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        deleteOrder();
                    }
                })
                .setNegativeButton("No",  new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        createCustomer();
                    }
                })
                .show();

}

Also remove one of ClickListener from your submitCustomer you have set twice clickListener to your submitCustomer

Upvotes: 0

shocking
shocking

Reputation: 908

To get your AlertDialog to display, you need to do a few more steps.

AlertDialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("Confirm")
        // add the rest of your setXYZ() methods here
        .create(); // this will create your AlertDialog object.
alertDialog.show(); // and this displays it!

Upvotes: 0

Related Questions