How to include "don't ask me again" popup dialog in an android app

Posted On // Leave a Comment

This post takes you through as to on how to include a "don't ask me again" popup dialog in your android app.I have successfully put this in my app. So you may ask any doubts you may have via the comments section below.


Logic: It sets a value in Android Preferences and checks it to determine whether it will show the dialog or not.
Tested in: Android Studio 1.0.1 and earlier
create checkbox.xml in resources/layoutsand paste the following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >
    <CheckBox
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok please do not show again." >
    </CheckBox>
</LinearLayout>
Paste the following where you want the box to appear
        AlertDialog.Builder adb = new AlertDialog.Builder(Youractivity.this);
        LayoutInflater adbInflater = LayoutInflater.from(Youractivity.this);        
        dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
        adb.setView(eulaLayout); adb.setIcon(R.drawable.yourimage);
        adb.setTitle("Attention");
        adb.setMessage("Your message here");
        adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                // Commit the edits!
                editor.commit();
            }
        });

        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";
                if (dontShowAgain.isChecked())
                    checkBoxResult = "checked";
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                // Commit the edits!
                editor.commit();
            }
        });
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String skipMessage = settings.getString("skipMessage", "NOT checked");
        if (!skipMessage.equals("checked"))
            adb.show();
SO, I did "copy and paste" too. Changed only the message strings. It works beautifully.

The following screenshot is from Facts! app, you can download it from here.


I'm including the code here with reference to the original author's site.

0 comments :

Post a Comment