I am trying to make a dialog that has a message and a checkbox. I am using the following code
private void displayWarning() {
SharedPreferences prefs;
final String PREFS_NAME = "UserData";
final String PREF_SHOW_WARNING_KEY = "show_warning";
prefs = this.getActivity().getSharedPreferences(PREFS_NAME, 0);
final String[] items = {"do not show again"};
final boolean[] itemsChecked = {false};
boolean displayWarnings = prefs.getBoolean(PREF_SHOW_WARNING_KEY, true);
if (displayWarnings) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("this is a warning")
.setCancelable(false)
.setMultiChoiceItems(items, itemsChecked, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
itemsChecked[which] = false;
} else {
itemsChecked[which] = true;
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemsChecked[0]) {
displayWarnings = false;
}
else {
displayWarnings = true;
}
prefs.edit().putBoolean(PREF_SHOW_WARNING_KEY, displayWarnings).commit();
}
When running this code, the checkbox and the text "do not show again" doens't get displayed. When I remove the message ("this is a warning"), I do get the checkbox. Also when I change the message to a title, I do get the checkbox, but the original message is too long to use in a title...
Hope this makes sence. The bottom line is that I want a dialog with both a message and a checkbox for the user to check, so that the dialog will never be shown again.