RateDialog_ICS

Before I show you the code I used to create this good looking “rate my app” dialog boxes I want you to know that these can be annoying and don’t recommend them in all situations.

Guide lines I use:

  • Don’t prompt until 5 days after the user has installed it.
  • Don’t prompt until the app has been launched 7 times.
  • Only include it if the app is free.

So for the code.

First create a new class and call it AppRater and set up some constants.

public class AppRater {

    

    //Change for your needs!

    private final static String APP_TITLE = "Test App";     

    private final static String APP_PACKAGE_NAME = "com.example.test_app";

    

    private final static int DAYS_UNTIL_PROMPT = 5;

    private final static int LAUNCH_UNTIL_PROMPT = 7;

 

    public static void app_launched(Context mContext){

    

    }

 

    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor){

 

    }

}

So far we created a new class and added some constants that you can change as needed. To make it more plug and play you could create the Strings APP_NAME and APP_PACKAGE_NAME and assign their values in the app_launch section by using something like below.

APP_PACKAGE_NAME = mContext.getApplicationInfo().packageName;

So let’s look at the code in the app_launched section.

public static void app_launched(Context mContext){

        SharedPreferences prefs = mContext.getSharedPreferences("rate_app", 0);

        if (prefs.getBoolean("dontshowagain", false)){return;}

        

        SharedPreferences.Editor editor = prefs.edit();

        

        //Add to launch Counter

        long launch_count = prefs.getLong("launch_count", 0) +1;

        editor.putLong("launch_count", launch_count);

        

        //Get Date of first launch

        Long date_firstLaunch = prefs.getLong("date_first_launch",0);

        if (date_firstLaunch == 0){

            date_firstLaunch = System.currentTimeMillis();

            editor.putLong("date_first_launch", date_firstLaunch);

        }

        

        //Wait at least X days to launch

        if (launch_count >= LAUNCH_UNTIL_PROMPT) {

            if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)){

                showRateDialog(mContext, editor);

            }

        }

        

        editor.commit();

        

    }

That section should be pretty straight forward. Create shared preferences, launch counter and date first launched.  If the launch counter is greater or equal to the value set earlier, and it has been at least 5 days defined by DAYS_UNTIL_PROMPT, show the rate dialog. 

Next we will work on creating the dialog box and the event handler for the dialog. Dialog will basically have 3 buttons and each one needs to have an on click event handler. So let’s look at the code.

public static void showRateDialog(final Context mContext,

            final SharedPreferences.Editor editor) {

        Dialog dialog = new Dialog(mContext);

 

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        String message = "If you enjoy using "

                + APP_TITLE

                + ", please take a moment to rate the app. Thank you for your support!";

        builder.setMessage(message)

                .setTitle("Rate " + APP_TITLE)

                .setIcon(mContext.getApplicationInfo().icon)

                .setCancelable(false)

                .setPositiveButton("Rate Now",

                        new DialogInterface.OnClickListener() {

 

                            @Override

                            public void onClick(DialogInterface dialog,

                                    int which) {

                                editor.putBoolean("dontshowagain", true);

                                editor.commit();

                                mContext.startActivity(new Intent(

                                        Intent.ACTION_VIEW, Uri

                                                .parse("market://details?id="

                                                        + APP_PACKAGE_NAME)));

                                dialog.dismiss();

                            }

                        })

                .setNeutralButton("Later",        

                        new DialogInterface.OnClickListener() {

 

                            @Override

                            public void onClick(DialogInterface dialog,

                                    int which) {

                                dialog.dismiss();

 

                            }

                        })

                .setNegativeButton("No, Thanks",

                        new DialogInterface.OnClickListener() {

 

                            @Override

                            public void onClick(DialogInterface dialog,

                                    int which) {

                                if (editor != null) {

                                    editor.putBoolean("dontshowagain", true);

                                    editor.commit();

                                }

                                dialog.dismiss();

 

                            }

                        });

        dialog = builder.create();

 

        dialog.show();

    }

So we create a dialog with a title, icon and a message and make it so the user can not cancel the dialog without selecting one of the 3 buttons.

  • Rate Now – sets preferences so it will not show the dialog again and opens the Google Play Store to your app so the user can rate it.
  • Later – just dismisses the dialog. Here you can add code to not show it again for x amount of times. If you leave it like this the user will be prompted each time the application is opened until they select “Rate Now” or “No, Thanks”
  • No, Thanks – sets preferences to not show the dialog again.

Now all you have to do is include it in your activity’s onCreate method like so.

AppRater.app_launched(this);

That’s it. If you would like to see a how to create a more customizable “rate my app” dialog check out the following snippet on AndroidSnippets. As you will see, that article gave me the idea for my article. If you want a more customizable view it is a good place to start but for my needs an AlertDialog did the trick.

Since these dialog boxes can be annoying and as much as we want public praise for an app it may have a negative impact. So what I recommend is if your app already has analytics built in use it to measure the impact the dialog has. Add event tracking code to measure how the dialog is used. You could set a value of 1 for each time a user clicks on “Rate Now” and 0 for the two other buttons this way when you average the values of the event you would see the impact. After adding the dialog you would also want to keep an eye on how it is impacting your ratings. If it is impacting it negatively you should consider improving the app as first priority and if you have no idea where the improvement is needed you maybe better of removing the dialog.

RateDialog_DroidX2_3_3
Droid X 2.3.3