Android: How to create a “Rate this app” button

If you have published your app you might want to get a bit more feedback from your users. In Google Play (formally known as Android Market) users can rate an app and leave a comment. This helps you as a developer and allows other users to see if your app is worth installing, also it might get you higher in the search result pages of Google Play.

The best way to get more ratings is to add a “Rank this app” button in your app. In this post I will show you how to implement the button.

Find your id in Google Play

First you will have to know your id in Google Play. To find out search your app in Google Play and check the url.
For example:
https://play.google.com/store/apps/details?id=com.cubeactive.qnotelistfree
This is the url of one of my apps. The id is this part: com.cubeactive.qnotelistfree
Note this id because we will need it in our code.

Rate me code

Now add a button to a layout and create an activity, add the following code to your activity. Replace [Id] with your app id.

private boolean MyStartActivity(Intent aIntent) {
    try
    {
    	startActivity(aIntent);
    	return true;
    }
    catch (ActivityNotFoundException e)
    {
    	return false;
    }
}

//On click event for rate this app button
public void btnRateAppOnClick(View v) {
	Intent intent = new Intent(Intent.ACTION_VIEW);
	//Try Google play
	intent.setData(Uri.parse("market://details?id=[Id]"));
	if (!MyStartActivity(intent)) {
		//Market (Google play) app seems not installed, let's try to open a webbrowser
		intent.setData(Uri.parse("https://play.google.com/store/apps/details?[Id]"));
		if (!MyStartActivity(intent)) {
			//Well if this also fails, we have run out of options, inform the user.
			Toast.makeText(this, "Could not open Android market, please install the market app.", Toast.LENGTH_SHORT).show();
		}
	}
}

Here is an example with my app as id:

//Try Google play
intent.setData(Uri.parse("market://details?id=com.cubeactive.qnotelistfree"));
if (!MyStartActivity(intent)) {
	//Market (Google play) app seems not installed, let's try to open a webbrowser
	intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.cubeactive.qnotelistfree"));
	if (!MyStartActivity(intent)) {
		//Well if this also fails, we have run out of options, inform the user.
		Toast.makeText(this, "Could not open Android market, please install the market app.", Toast.LENGTH_SHORT).show();
	}
}


Code explained

Let’s take a closer look at the code.

The MyStartActivity function helps start an activity. This function tries to start the activity and catches the error if the activity is not installed. This function makes the code in our onclick event more compact and easy to expand when implementing new markets:

private boolean MyStartActivity(Intent aIntent) {
    try
    {
    	startActivity(aIntent);
    	return true;
    }
    catch (ActivityNotFoundException e)
    {
    	return false;
    }
}

Now to continue to our onclick event.
The code first try’s to open the Market app:

Intent intent = new Intent(Intent.ACTION_VIEW);
//Try Google play
intent.setData(Uri.parse("market://details?id=com.cubeactive.qnotelistfree"));
if (!MyStartActivity(intent)) {

MyStartActivity will return true if the Market app is started, if it is not installed then MyStartActivity will result false and the code will try to open the Google Play website:

//Market (Google play) app seems not installed, let's try to open a webbrowser
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.cubeactive.qnotelistfree"));
if (!MyStartActivity(intent)) {

If this also fails the code will show a toast message to inform the user:

//Well if this also fails, we have run out of options, inform the user.
Toast.makeText(this, "Could not open Android market, please install the market app.", Toast.LENGTH_SHORT).show();

Wrapping up

That’s all for this post, good luck and hopefully this will get your app some great ratings.

For more information see: http://developer.android.com/guide/publishing/publishing.html

2 comments on “Android: How to create a “Rate this app” button

Leave a Reply

Your email address will not be published. Required fields are marked *