Search code examples
androidandroid-alertdialog

android - alert dialog builder confirmation button


In my app I have used an alert dialog builder for button click. So when I press a button, little pop up is opened with options - Activate and Deactivate. So can i make that when a user press Activate or Deactivate, Confirmation widnows open with question are you sure ? and a choice yes or no ??


Solution

  • I have created dialog inside another dialog.

    See this code:

    twsbiSelectionMenuDialog = new Dialog(this,R.style.CustomDialogTheme);
                twsbiSelectionMenuDialog.setContentView(R.layout.twsbi_selection_menu_dialog);
                twsbiSelectionMenuDialog.setCancelable(true);
                twsbiSelectionMenuDialog.setCanceledOnTouchOutside(true);
                
                // To Open new Canvas ===================================
                Button newLayoutButton = (Button) twsbiSelectionMenuDialog.findViewById(R.id.newLayoutButton);
                newLayoutButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        twsbiSelectionMenuDialog.dismiss();
                        // AlertDialog for confirmation
                        AlertDialog.Builder clearConfirmDialog = new AlertDialog.Builder(TWSBIDrawMainActivity.this);
                        clearConfirmDialog.setMessage("Do you want to clear this and open new canvas ?").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Action for 'Yes' Button
                                myView = new MyView(TWSBIDrawMainActivity.this);
                                takePhotoFromCamera = false;
                                takePhotoFromGallery = false;
                                canvasColor = 0xFFFFFFFF;
                                drawingLayout.removeView(myView);
                                drawingLayout.addView(myView);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //  Action for 'NO' Button
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = clearConfirmDialog.create();
                        alert.setTitle("Draw");
                        alert.setIcon(R.drawable.app_icon);
                        alert.show();
                    }
                });
                
                // For canvas Color Selection ===================================
                Button canvasColorButton = (Button) twsbiSelectionMenuDialog.findViewById(R.id.canvasColorButton);
                canvasColorButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        twsbiSelectionMenuDialog.dismiss();
                        pickColour(); // to pick colour
                    }
                });
    

    Just refer that for your condition and it will help you.