0

I have a PreferenceScreen in which i have a checkbox with the following code:

<CheckBoxPreference
        android:defaultValue="false"
        android:key="the_key"
        android:summary="Random text here."
        android:title="My Title"
        />

I want a pop up message to be displayed on click of this checkbox that will have a set button and a cancel. If "set" will be pressed then the checkbox will be checked if previously unchecked and the opposite. How can i do that? Here is the code of my pop up message.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Some message that will be displayed")
            .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do something
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}

Also how can i get the value of my checkbox(checked or not)? Thanks!!

1 Answers1

0

To get checkbox is checked or not from CheckBoxPreference, use this

CheckBoxPreference pref = (CheckBoxPreference) findPreference("checkbox_preference");
pref.isChecked(); //returns if it is checked or not
Fahim
  • 12,198
  • 5
  • 39
  • 57
  • In this code i have an error in the findPreference saying: Cannot resolve method findPreference. Note that I'm trying to use that in the onCreateDialog method. –  Feb 24 '15 at 14:29
  • 1
    use the proper keyname – Fahim Feb 24 '15 at 14:34
  • Done but still that doesn't solve my other problem! –  Feb 24 '15 at 16:08
  • What is the other problem – Fahim Feb 24 '15 at 17:06
  • As i mentioned i want a confirm dialog to be displayed in order for the value of the checkBoxPreference to be changed. Any idea how to do that? –  Feb 25 '15 at 08:34