Can't see how to disable an AlertDialog Box

82 views Asked by At

I'm trying to figure out how to disable definitely an AlertDialog Box when I press the negative/positive button. My current app works tapping the screen and I want my app to show an AlertDialog Box at the first tap. This Box says "attention, some of the names are invented!" and the buttons are "Ok!"(positive button) and "Don't remind this to me"(Negative Button). In both cases I want the app to stop showing the Box at each tap because it's obviously frustrating and it has no sense to show a message at each tap (consider the user is going to tap 1 or 2 times every 5 secs). So I'm trying to figure out how to disable it after the first tap.

For now, this is what I wrote

   public void tap(View view) {

    final AlertDialog.Builder tapAlert = new AlertDialog.Builder(this);
    tapAlert.setMessage("The names are mostly invented!");
    tapAlert.setPositiveButton("Ok!", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();

        }
    });
    tapAlert.setNegativeButton("Don't remind it to me", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


            dialog.cancel();

        }

    });
    tapAlert.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            // do nothing, just allow dialog to close
            // this happens on back button getting hit
        }
    });
    tapAlert.setTitle("Attention!");
    tapAlert.create();
    tapAlert.show();
1

There are 1 answers

4
codeMagic On BEST ANSWER

Just create a `boolean variable at class level

boolean firstTap = true;

Then change that after it has shown once

public void tap(View view) {

    final AlertDialog.Builder tapAlert = new AlertDialog.Builder(this);
    ...
    if (firstTap) {
        tapAlert.show();
        firstTap = false;
    }

This will work if you want it to show the first time every time the app runs. If you only ever want it to show on the first tap the save the boolean in SharedPreferences and check that when the Activity starts.

Even better would be to check that variable before calling the tap() method. Don't call it when firstTap is false.