Skip to content
Advertisement

How to make Bottom Sheet Dialog that shows one time only

I want to create a Bottom Sheet Dialog. It looks like the image below enter image description here

public class MainActivity extends AppCompatActivity {


    //this is Button that open dialog
    private Button button;


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

        button = findViewById(R.id.nextBtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
                        MainActivity.this, R.style.BottomSheetDialogTheme);
                View bottomSheetView = LayoutInflater.from(getApplicationContext())
                        .inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
                bottomSheetDialog.setContentView(bottomSheetView);


                bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.exit(0);
                    }
                });
                bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(MainActivity.this, Activity2.class));
                        finish();
                    }

                });
            }

        });
    }
}

I have tried many methods such as SharedPreferences. But it not working

Advertisement

Answer

        SharedPreferences preference = getSharedPreferences("mPreference", MODE_PRIVATE);
    boolean isUserAgreed = preference.getBoolean("isUserAgreed", false);
    if (!isUserAgreed){
        // show bottomSheetDialog
    } else {
        startActivity(new Intent(MainActivity.this, Activity2.class));
        finish();
    }

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
                    MainActivity.this, R.style.BottomSheetDialogTheme);
            View bottomSheetView = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
            bottomSheetDialog.setContentView(bottomSheetView);


            bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.exit(0);
                }
            });
            bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences.Editor editor = getSharedPreferences("mPreference", MODE_PRIVATE).edit();
                    editor.putBoolean("isUserAgreed", true);
                    editor.apply();
                    startActivity(new Intent(MainActivity.this, Activity2.class));
                    finish();
                }

            });
        }

    });
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement