Last Updated on February 6, 2018
Hi and welcome to another tutorial of codingdemos, in this tutorial you will learn how to create android custom alertdialog with never show again checkbox. We are going to build alertdialog that will show an example of app changelog for specific version, so let’s get started!
In this tutorial we will be using the following:
- Android studio version 2.3.3
- Android emulator Nexus 5X with API 24
- Minimum SDK API 16
The final result will look something like this:
- Open up Android Studio and create a new project and give it a name, in our case we’ve named it (CustomDialogCheckbox), choose API 16 as the minimum SDK, then choose blank activity and click on finish and wait for Android Studio to build your project
- Now let’s create a layout xml file that we will use it for android custom alertdialog and in our case we named it (dialog_app_updates.xml)
- Inside the dialog layout file we will add a checkbox with a text saying “Don’t show this message again” as shown below:
<CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Don't show this message again"/>
- Here is the full code for (dialog_app_updates.xml) file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Don't show this message again" /> </LinearLayout>
Note: The reason we added padding to the root layout because we don’t want the checkbox to be sticked to the edge of the screen.
- Inside (MainActivity.java) file we will start working on android custom alertdialog, we initialize the alertdialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
- Then we need to initialize the (dialog_app_updates.xml) file so that we can use it for android custom alertdialog like this:
View mView = getLayoutInflater().inflate(R.layout.dialog_app_updates, null);
- Let’s initialize the Android checkbox through the view that we’ve just created it like this:
CheckBox mCheckBox = mView.findViewById(R.id.checkBox);
Note: We have to use (mView) to be able to reference the checkbox inside the (dialog_app_updates.xml) file.
- Then we give the 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 view that holds the (dialog_app_updates.xml) file to android custom alertdialog like this:
mBuilder.setView(mView);
- Let’s add a button to the alertdialog so that when the user taps on the button it will dismiss/hide the alertdialog like this:
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } });
- Finally we need to show Android alertdialog to the user screen and we can do that like this:
AlertDialog mDialog = mBuilder.create(); mDialog.show();
Figure (1): Final output
- Now run the app and you will be able to see android custom alertdialog show up in the screen as shown in figure (1), but one thing you will notice is when you tap on the checkbox (Don’t show this message again), then you exist the app and then run it back again and you will see the alertdialog showing up again! What we wanted was when you tap on the checkbox then you exist the app and then run it back again you will not see the alertdialog!! Hmm…
- The reason why this is happening because we are not storing the status of checkbox in order to determine whether the user have actually tapped on the checkbox or not to decide whether to show or hide android custom alertdialog!
- To be able to solve this issue we are going to use Android sharedpreference in order to store the status of checkbox whether it was checked or not and act accordingly whether to show or hide the alertdialog.
Note: If you are new to Android sharedpreference, then i suggest you to watch my tutorial where i show how to use android sharedpreference
- Now let’s create 2 methods one is for storing and the other one is for retrieving the checkbox status from android sharedpreference.
- Now let’s work on storing the checkbox status in sharedpreference by creating a method and in our case we have named it (storeDialogStatus) which takes a boolean value as an argument like this:
private void storeDialogStatus(boolean isChecked){ SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE); SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putBoolean("item", isChecked); mEditor.apply(); }
- Next let’s create the other method which retrieve the checkbox status from sharedpreference like this:
private boolean getDialogStatus(){ SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE); return mSharedPreferences.getBoolean("item", false); }
- Now we are done with sharedpreference part, next we need to handle the part where we check whether the checkbox was checked or not and we do that by using checkbox setOnCheckedChangeListener like this:
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { } });
- Inside the onCheckedChanged method is where we are going to store the status of the checkbox in sharedpreference like this:
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(compoundButton.isChecked()){ storeDialogStatus(true); }else{ storeDialogStatus(false); } } });
- Now that we have managed to store the checkbox status in sharedpreference, next we need to act accordingly whether to show or hide the android custom alertdialog like this:
if(getDialogStatus()){ mDialog.hide(); }else{ mDialog.show(); }
- The full code for the (MainActivity.java) file is shown below:
package com.codingdemos.customdialogcheckbox; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this); View mView = getLayoutInflater().inflate(R.layout.dialog_app_updates, null); CheckBox mCheckBox = mView.findViewById(R.id.checkBox); 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."); mBuilder.setView(mView); mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); AlertDialog mDialog = mBuilder.create(); mDialog.show(); mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(compoundButton.isChecked()){ storeDialogStatus(true); }else{ storeDialogStatus(false); } } }); if(getDialogStatus()){ mDialog.hide(); }else{ mDialog.show(); } } private void storeDialogStatus(boolean isChecked){ SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE); SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putBoolean("item", isChecked); mEditor.apply(); } private boolean getDialogStatus(){ SharedPreferences mSharedPreferences = getSharedPreferences("CheckItem", MODE_PRIVATE); return mSharedPreferences.getBoolean("item", false); } }
That’s it we are done for this tutorial 😀
– The source code for this tutorial is available on this link: https://github.com/codingdemos/CustomDialogCheckBox
– For more cool video please check out the YouTube channel on this link: https://www.youtube.com/c/CodingDemos