Is there a way for a user to disable an AlertDialog completely?

Posted by NewGuyChris on Stack Overflow See other posts from Stack Overflow or by NewGuyChris
Published on 2013-11-12T03:01:35Z Indexed on 2013/11/12 3:54 UTC
Read the original article Hit count: 158

In the app I'm making, I have an "if" statement where if two strings are saved to a certain string, an AlertDialog pops up. These strings will stay the same for some users, thus having this AlertDialog constantly pop up whenever they launch the activity where the ALertDialog is set to appear. Code (I have no setNegativeButton as of yet):

 private void SetWarning() {

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Warning!");
    alert.setMessage(R.string.Warning);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        //No action needed; just close the AlertDialog.
            }
        });

alert.show(); } 

Here is a segment of my code that makes this AlertDialog appear:

        SharedPreferences sharedPreferences = getSharedPreferences("MY_PREF", MODE_PRIVATE);
        String s = sharedPreferences2.getString("MEM1", "");
        String s2 = sharedPreferences2.getString("MEM2", "");

    if(s.equals("String1") && s2.equals("String2"))
        SetWarning();

Is there a way to make an "alert.setNegativeButton" method where if the user clicks it, the AlertDialog will NEVER appear again? I'm thinking of maybe somehow implementing another SavedPreferences somehow so it saves the users selection and will then prevent the AlertDialog from ever appearing again. So far, to no luck. I've searched to find nothing, other than people asking how to disable buttons in an AlertDialog.

Thank you!

New updated code:

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //set sharedpreferences boolean called DONTSHOWAGAIN to true;
            SharedPreferences sharedPreferences2 = getSharedPreferences("MY_PREF", MODE_PRIVATE);
            Boolean dontShowAgain = sharedPreferences2.getBoolean("dontShowAgain ", false);
            SharedPreferences.Editor ed = sharedPreferences2.edit();
            ed.putBoolean("dontShowAgain", true);
            ed.commit();
        }
    });

alert.show(); } 

private void StringWarning() {



        SharedPreferences sharedPreferences2 = getSharedPreferences("MY_PREF", MODE_PRIVATE);
        String s = sharedPreferences2.getString("MEM1", "");
        String s2 = sharedPreferences2.getString("MEM2", "");


    if(s.equals("String1") && s2.equals("String2")){
        if(!dontShowAgain){
            SetWarningExamConflict();
        }
        }

© Stack Overflow or respective owner

Related posts about android

Related posts about android-alertdialog