0

I'm trying to do alert message by disable ok and cancel button if the checkbox is unchecked.

reconfirm.java:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
                LayoutInflater layoutInflater 
                 = (LayoutInflater)getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE);  
                View popupView = layoutInflater.inflate(R.layout.popup, null);


                alertDialogBuilder.setView(popupView);

                CheckBox check= (CheckBox)findViewById(R.id.checkBox1);  

            if (check.isChecked() ) {

                    AlertDialog dialog = null;
                    ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
                }




                alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {

                          Intent intObj = new Intent(getApplicationContext(),
                                agree.class);
                        startActivity(intObj);
                      }
                  });


                alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {


                     Intent intObj = new Intent(getApplicationContext(),
                                IntentExampleActivity.class);
                            startActivity(intObj);


                  }

                  });

                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146

3 Answers3

0

Try Using setEnabled() and setClickable method for buttons.

here is the doc

user3717646
  • 436
  • 3
  • 10
0

Try following code snippets. Hope you get some idea from this :)

if (check.isChecked()) {
 alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
 alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}

where dialog is object of AlertDialog.

Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • what is the different between using AlertDialog alertDialog = alertDialogBuilder.create(); with final AlertDialog dialog = builder.create();? – user3714182 Aug 25 '14 at 04:17
  • @user3714182 I have updated my answer.My intention was to tell that it is object of AlertDialog.I have just answered with reference to your code.Did it worked for u? – Harshal Benake Aug 25 '14 at 06:14
  • no..i'm tyring to write .setEnabled(false); after the alertDialog.show(); ...there is no error displyed but in the device it show "unfortunately your app has stopped" – user3714182 Aug 25 '14 at 07:19
  • Can u print ur logcat please? – Harshal Benake Aug 25 '14 at 10:40
0

This is answered already here--How to disable / enable dialog negative positive buttons?

after dialog.show use below code

if(your_condition_true) dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button

Community
  • 1
  • 1
NIPHIN
  • 1,071
  • 1
  • 8
  • 16
  • write a checkbox listener (setOnCheckedChangeListener). in it if(isChecked){ dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);}else { dialog.getButton(AlertDialog.BUTTON1).setEnabled(true);} – NIPHIN Aug 25 '14 at 12:06