2

Here is my Dialog box

 

public class CustomDialogClass extends Dialog implements
 android.view.View.OnClickListener {

     public Activity c;
     public Dialog d;
     public Button no;
     CheckBox yes;
     boolean p;
     public CustomDialogClass(Activity a) {
         super(a);
         // TODO Auto-generated constructor stub
         this.c = a;
     }

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.custom_dialog);
         yes = (CheckBox) findViewById(R.id.checkBox1);
         no = (Button) findViewById(R.id.btn_no);
         no.setOnClickListener(this);
         yes.setChecked(false);
         yes.setOnClickListener(this);
     }

     @Override
     public void onClick(View v) {
         switch (v.getId()) {
         case R.id.checkBox1:
             yes.setChecked(true);
             break;
         case R.id.btn_no:
             dismiss();
             break;
         default:
             break;
         }
     }
 }

Now, I open the dialog and check the checkbox, click on button and dismiss the dialog. But When I open it again, the checkbox is again unchecked. what should i do?

2
  • The hint is given by onCreate(Bundle savedInstanceState). You need to save the check box values then pass them back in retrieving them from the savedInstanceState bundle. Commented Jul 25, 2013 at 13:37
  • It's no good practice to use Dialog nowadays. It's strongly recommended to use the DialogFragment approach. showDialog() is deprecated since API level 13. Please see my answer below. Commented Jul 25, 2013 at 13:43

2 Answers 2

6

The way you are using your dialog is not recommended anymore! You should consider using a DialogFragment.

The problem why you are loosing your checked state is because the dialog is recreated when you open it again.

See this example of the DialogFragment approach http://developer.android.com/reference/android/app/DialogFragment.html. You will see that you can pass arguments to the dialog.

For a solution I recommend that when you close the dialog you pass the selected value back to the hosting activity... when the dialog should be reopenned you just pass the arguments to the dialog so that the dialog sets its default selection.

Edit:

  1. Since you would like to display checkboxes I will take and adapt the example code from http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList for checkboxes. It's still convenient to use the AlertDialog.Builder.

  2. Wrap the Builder into a DialogFragment by overwriting the onCreateDialog-method. You can pass the list of selected items via the Bundle as boolean array and then use is for setMultiChoiceItems.

    public class CheckBoxAlertDialogFragment extends DialogFragment {
        public static CheckBoxAlertDialogFragment newInstance(boolean[] checkedItems) {
            CheckBoxAlertDialogFragment frag = new CheckBoxAlertDialogFragment();
            Bundle args = new Bundle();
            args.putBooleanArray("checkedItems", checkedItems);
            frag.setArguments(args);
            return frag;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final boolean[] checkedItems = getArguments().getBooleanArray("checkedItems");
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Set the dialog title
        builder.setTitle(R.string.pick_toppings)
                // Specify the list array, the items to be selected by default (null for none),
                // and the listener through which to receive callbacks when items are selected
                .setMultiChoiceItems(R.array.toppings, checkedItems,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which,
                                                boolean isChecked) {
                                checkedItems[which] = isChecked;
                            }
                        })
                // Set the action buttons
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK, so save the checkedItems results somewhere
                        // or return them to the component that opened the dialog
                        //...
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //...
                    }
                });
    
        return builder.create();
    }
    }
    
  3. In your activity you should have a variable, e.g. named checkedItems, of type boolean[] somewhere which saves the checkbox states. You can open the dialog then with:

    void showDialog() {
            DialogFragment newFragment = CheckBoxAlertDialogFragment.newInstance(checkedItems);
            newFragment.show(getFragmentManager(), "dialog tag");
        }
    
Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry. I still can't get it. can you show me the code where you are passing the states of checkboxes. I am new to android and dialog boxes especially. It will be very helpful
1

Your check box is going to be unchecked each time you show/create the dialog because in its onCreate method you have yes.setChecked(false);. Instead of just dismissing the dialog you should save the checkbox's value before dismissing and get that value to reset the check box each time the dialog is created. You could use SharedPreferences for this which will retain the value even if your Activity is destroyed or pass the value back and forth as needed with your main activity.

Thanks to @andd, I had forgotten that Dialog is deprecated and he is right that you should be using the DialogFragment in which case the SharedPreferences wouldn't be necessary.

2 Comments

But I think DialogFragment requires minimum API 11 but in my project min API is 8
You could use the Support Fragments which are for devices prior to Android 3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.