-2

Hi i'm trying to create dialog contains two buttons [ GOT IT ! ] and [ DON'T SHOW ME THIS AGAIN ] i want when i click [ DON'T SHOW ME THIS AGAIN ] button dialog Completely closed And do not show again at application Reopen

enter image description here

public boolean Show = false;

public void IntroSupport(){

    Show = true;

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_launcher);
    builder.setIcon(R.mipmap.service)
            .setTitle("Online Support :")
            .setView(image)
            .setMessage("Some text")
            .setNegativeButton("GOT it!",null)
            .setPositiveButton("Don't Show Me this Again", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
    builder.setCancelable(false);
    AlertDialog about = builder.create();
    about.show();
    TextView messageText = (TextView) about.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    Button nbutton = about.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(Color.BLACK);
}


        @Override
        public void onPageSelected(int position) {
            switch (position){
                case 0:
          getSupportActionBar().setTitle(Html.fromHtml("title :"+Pb));
                    tabLayout.getTabAt(0).setIcon(R.mipmap.ic1vrai);
                    tabLayout.getTabAt(1).setIcon(R.mipmap.ic__2vrai);
                    break;
                case 1:

                    if(Show == false){
                        IntroSupport();
                    }

                    getSupportActionBar().setTitle(Html.fromHtml("("titl"+sus));
                    tabLayout.getTabAt(1).setIcon(R.mipmap.ic_2faux);
                    tabLayout.getTabAt(0).setIcon(R.mipmap.ic1faux);
                    break;
            }
        }
ALiiLA
  • 55
  • 1
  • 11
  • Possible duplicate of [How to show only one dialog at a time?](http://stackoverflow.com/questions/12560931/how-to-show-only-one-dialog-at-a-time) – ALiiLA Mar 31 '17 at 14:55

2 Answers2

4

Use SharedPreference for showing dialog or not. Try this code:

public boolean Show = false;

public void IntroSupport(){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_launcher);
    builder.setIcon(R.mipmap.service)
            .setTitle("Online Support :")
            .setView(image)
            .setMessage("Somme text")
            .setNegativeButton("GOT it!",null)
            .setPositiveButton("Don't Show Me Again", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = getSharedPreferences("mypref", MODE_PRIVATE).edit();
                     editor.putBoolean("dontshow", true);
                     editor.commit();
                }
            });
    builder.setCancelable(false);
    AlertDialog about = builder.create();
    about.show();
    TextView messageText = (TextView) about.findViewById(android.R.id.message);
    messageText.setGravity(Gravity.CENTER);
    Button nbutton = about.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(Color.BLACK);
}


    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
          getSupportActionBar().setTitle(Html.fromHtml("title :"+Pb));
                tabLayout.getTabAt(0).setIcon(R.mipmap.ic1vrai);
                tabLayout.getTabAt(1).setIcon(R.mipmap.ic__2vrai);
                break;
            case 1:
                SharedPreferences prefs = getSharedPreferences("mypref", MODE_PRIVATE); 
                show = prefs.getBoolean("dontshow", false);
                if(Show == false){
                    IntroSupport();
                }

                getSupportActionBar().setTitle(Html.fromHtml("("titl"+sus));
                tabLayout.getTabAt(1).setIcon(R.mipmap.ic_2faux);
                tabLayout.getTabAt(0).setIcon(R.mipmap.ic1faux);
                break;
        }
    }
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
2

You can store a value to the SharedPreferences if the user press on the Don't show me this again button. Then you should wrap your dialog stuff in an if clause which checks if the value in the SharedPreferences is set or not.

Here is the link to the documentation of the SharedPreferences: https://developer.android.com/reference/android/content/SharedPreferences.html

Neoklosch
  • 406
  • 3
  • 10