Android custom alertdialog with never show again checkbox (Demo)

Опубликовано: 28 Сентябрь 2024
на канале: Coding Demos
4,661
12

In this tutorial you will learn how to create Android custom alertdialog and how we can use a checkbox in order to hide the android alertdialog from showing up in the screen when launching the app again.

- The link to the full tutorial is available here: http://www.codingdemos.com/android-cu...

Here is how to do it:

- First we will create a layout xml file where we will add the android checkbox and we name that file (dialog_app_updates)

- Now we create android alertdialog using alertdialog builder inside (MainActivity.java) like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);

- Next is we need to initialize the xml layout that have the checkbox like this:
View mView = getLayoutInflater().inflate(R.layout.dialog_app_updates, null);

- Then we can declare the checkbox by using mView to be able to reference the checkbox in the layout file like this:
CheckBox mCheckBox = mView.findViewById(R.id.checkBox);

- We can give android alertdialog a title and a message like this:
mBuilder.setTitle("What's new in V1.0");
mBuilder.setMessage("1- Lorem ipsum dolor sit amet. \n2- consectetur adipiscing elit. \n3- Vestibulum vulputate fringilla justo. \n4- nec varius magna suscipit ac.");

- Now we need to set the custom layout to the alertdialog, otherwise the dialog box will be empty:
mBuilder.setView(mView);

- We need to declare the android alertdialog positive button to be able to hide the alertdialog

- Then we need to handle checkbox status by using CheckBox.setOnCheckedChangeListener

- Inside checkbox onCheckedChanged method we will use android sharedpreference to be able to store the checkbox status

- Now we can show android alertdialog like this:
AlertDialog mDialog = mBuilder.create();
mDialog.show();

- That's it we are done.