Take a rate, take a dialog!

10 July 2012

Just made a small and quick component for Android.

It is a dialog that pop up after a certain number of day and app launches, to ask the user to rate the app in Google Play.

Eventually it can show a “More” button to display the other apps of the author in Google Play.

It is a subclass of DialogFragment and I also made a version with the Support Library (V4).

If you have any suggestions / opinions, please jot them down as comments in this post. 🙂

I tried to make this dialog to be easiest to use as possible.

So this post is also some sort of tutorial about using DialogFragment (or at least an attempt :-P).

It is open source, of course, with Apache License 2.0.

Please, use dialogs carefully, as they can worsen user experience.

Read this post of Juhani Lehtimäki for more.



In my opinion, a “rate me” feature can still be implemented in an app, as a part of an already existing view or existing dialog, as the About or the Changelog.



In my github repository, there are four projects:

RateMeDialog (The “real” library project) RateMeDialogV4 RateMeDialogTest RateMeDialogTestV4

V4 means that the project use the Support Library to let old versions of Android to run it.

The Test projects are there only as samples.

In the end if you want to support only Ice Cream Sandwich and above, then RateMeDialog is the project you have to use, otherwise choose RateMeDialogV4 .

How to use

To use the dialog there are three steps to follow:

Use the project as a library project. (In Eclipse, right click on your project, “Android”, in “Library” section choose “Add”) Change the value of RateMeDialog.APPTAG. Just call RateMeDialog.showRateMeDialog() accordingly.

Changing RateMeDialog.APPTAG is very important, as RateMeDialog use SharedPreferences to store the user response (to show the dialog later, to never show it again, etc.)

Customizing this variable will prevent your app to accidentally use the settings of other apps that may use (I hope so! :-)) this same class. I wrote the meaning of RateMeDialog.showRateMeDialog() ‘s parameters in the comments, anyway here is a copy & paste:

ownerManager – FragmentManager of the owner, the entity from which we want to show a dialog.

– of the owner, the entity from which we want to show a dialog. publisherName – If null then the relative button will be removed from the dialog.

– If null then the relative button will be removed from the dialog. appPackageName – The app to rate.

– The app to rate. title – The dialog title. For now the title is rendered as a separate TextView for more flexibility. If null then the relative View will be removed from the dialog.

– The dialog title. For now the title is rendered as a separate for more flexibility. If null then the relative will be removed from the dialog. total_days – Number of days to do before show this dialog. Pass < 0 along with total_launches < 0 to ignore RATEMEDIALOG_NEVER .

– Number of days to do before show this dialog. Pass < 0 along with total_launches < 0 to ignore . total_launches – Number of launches to do before show this dialog. Pass < 0 along with total_days < 0 to ignore RATEMEDIALOG_NEVER .

– Number of launches to do before show this dialog. Pass < 0 along with total_days < 0 to ignore . context – Normally it is the Activity from which you called this method.

RateMeDialogActivity class of project RateMeDialogTest is self explanatory on how to practically use RateMeDialog .

It is a simple app that shows the dialog automatically after some time or through a Button .

There is not much to say:

public class RateMeDialogTestActivity extends Activity { /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RateMeDialog.showRateMeDialog(getFragmentManager(), "Google Inc.",//or null "com.google.android.apps.translate", "Test",//or null 1,//1 Days. 3,//3 Launches. this); } public void onShowDialog(View view) { RateMeDialog.showRateMeDialog(getFragmentManager(), "Google Inc.",//or null "com.google.android.apps.translate", null, -1,//Show it immediately. -1,//Show it immediately. this); } }

DialogFragment

If you want to create your own dialog, here is a short to-do list:

Define the layout in an xml file, as any other layout. Extend DialogFragment . Override onCreateView() and in there inflate the layout. Eventually, retrieve the arguments with getArguments() . To show the dialog, create a new instance, set the arguments through a Bundle , retrieve a FragmentTransaction , using a FragmentManager , and use it to actually show the dialog: RateMeDialog dialog = new RateMeDialog(); Bundle params = new Bundle(); params.putString(RATEMEDIALOG_PUBLISHER, publisherName); params.putString(RATEMEDIALOG_APPAPCKAGENAME, appPackageName); params.putString(RATEMEDIALOG_TITLE, title); dialog.setArguments(params); //Show the dialog. FragmentTransaction ownerTransaction = ownerManager.beginTransaction(); dialog.show(ownerTransaction, RATEMEDIALOG_TAG); Put everything in a static method, if you want to encapsulate the showing method in your subclass of DialogFragment .