-1

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.

MWB
  • 1,830
  • 1
  • 17
  • 38

2 Answers2

1

The alert dialog fits to show just one type of message.

So, as I can understand, if you want to view either a text message than a multichoice, you must use a custom layout.

There have been many other similar question here on StackOverflow about the same item and everybody has the same suggestion: using a custom layout for "complex" layouts.

Watch these links:

Create AlertDialog with both MultiChoiceItems and message

Android : Alert Dialog with Multi Choice

Community
  • 1
  • 1
Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
0

Look at this example. I have an alert dialog with a checkbox. It works fine:

public class StartActivity extends AppCompatActivity {

SharedPreferences sharedpreferences;
String email_pref;
String passw_pref;
Boolean remember;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    sharedpreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
    email_pref = sharedpreferences.getString("EMAIL_SAVED", "");
    passw_pref = sharedpreferences.getString("PASSW_SAVED", "");
    remember = sharedpreferences.getBoolean("REMEMBER", false);
}


public void SignIn(View v){
   // dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    // custom dialog
    final Dialog dialog = new Dialog(StartActivity.this);
    dialog.setContentView(R.layout.sign_in_layout);    //il suo layout personalizzato
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    // set the custom dialog components - text, image and button
    final EditText email = (EditText) dialog.findViewById(R.id.editEmailSignIn);
    final EditText password = (EditText) dialog.findViewById(R.id.editPasswordSignIn);
    final CheckBox rememberCheck = (CheckBox) dialog.findViewById(R.id.check_in);


    if(remember == true && !email_pref.equals("") && !passw_pref.equals("")){
        email.setText(email_pref);
        password.setText(passw_pref);
        rememberCheck.setChecked(true);
    }

    Button dialogButton = (Button) dialog.findViewById(R.id.btn_confirm_signin);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mail = email.getText().toString();
            String psw = password.getText().toString();
            //QUERY AL DB PER VEDERE SE L'UTENTE ESISTE E ACCESSO EVENTUALE ALL'ACTIVITY SUCCESSIVA OPPURE ERRORE
            String query = "SELECT email, password FROM users WHERE email='" + mail + "' AND password = '" + psw + "'";
            final SQLiteDatabase db = openOrCreateDatabase("AuctionDB", MODE_PRIVATE, null);
            //db.execSQL(query);
            Cursor c = db.rawQuery(query, null);
            if(!c.moveToFirst()){
                Toast.makeText(getApplicationContext(),"No matches!", Toast.LENGTH_LONG).show();
            }else{
                //Toast.makeText(getApplicationContext(),"UTENTE TROVATO", Toast.LENGTH_LONG).show();
                dialog.dismiss();

                if(rememberCheck.isChecked()){
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString("EMAIL_SAVED", email.getText().toString());
                    editor.putString("PASSW_SAVED", password.getText().toString());
                    editor.putBoolean("REMEMBER", true);
                    editor.commit();
                }else{
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString("EMAIL_SAVED", "");
                    editor.putString("PASSW_SAVED", "");
                    editor.putBoolean("REMEMBER", false);
                    editor.commit();
                }

                //pass to the ViewActivity
                Intent intent = new Intent(StartActivity.this, ViewActivity.class);
                Bundle b = new Bundle();
                b.putString("email", mail);
                intent.putExtras(b);
                startActivity(intent);
                finish();
            }

        }
    });
    dialog.show();
}

the entire example is at:

https://github.com/alessandroargentieri/AuctionExample

Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
  • Although it is great to see another solution that works, I am also very interested what goes wrong in my example – MWB Nov 04 '16 at 10:46