Implementing Rate Us feature in Android

In this tutorial, you will learn how to implement a Rate Us feature in your Android application. Using this feature allows you to prompt a user to rate your Android application. Ratings are determined by Google Play users. Your application’s rating is one of the most important factors influencing its ranking in the various lists and search results in Google Play. It’s also one of the key signals that the editorial staff looks for when curating apps and games for promotion in the store. We will create an activity, and after the application launches twice the user will be prompted to rate the application. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project RateUsTutorial.

Application Name : RateUsTutorial

Project Name : RateUsTutorial

Package Name : com.androidbegin.rateustutorial

Then name your activity RateUsTutorial.java 

Open your RateUsTutorial.java and paste the following code.

RateUsTutorial.java

package com.androidbegin.rateustutorial;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the view from activity_main.xml
		setContentView(R.layout.activity_main);
		// Launch RateUs.java class
		RateUs.app_launched(this);

		// Your Codes...

	}

}

In this activity, we have inserted a function to call RateUs.java class below the setContentView.

Next, create a new class RateUs.java. Go to File > New > Class and name it RateUs.java. Select your package named com.androidbegin.rateustutorial and click Finish.

Open your RateUs.java and paste the following code.

RateUs.java

package com.androidbegin.rateustutorial;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RateUs {
	// Insert your Application Title
	private final static String TITLE = "RateUs Tutorial";

	// Insert your Application Package Name
	private final static String PACKAGE_NAME = "com.androidbegin.rateustutorial";

	// Day until the Rate Us Dialog Prompt(Default 2 Days)
	private final static int DAYS_UNTIL_PROMPT = 0;

	// App launches until Rate Us Dialog Prompt(Default 5 Launches)
	private final static int LAUNCHES_UNTIL_PROMPT = 2;

	public static void app_launched(Context mContext) {
		SharedPreferences prefs = mContext.getSharedPreferences("rateus", 0);
		if (prefs.getBoolean("dontshowagain", false)) {
			return;
		}

		SharedPreferences.Editor editor = prefs.edit();

		// Increment 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_firstlaunch", 0);
		if (date_firstLaunch == 0) {
			date_firstLaunch = System.currentTimeMillis();
			editor.putLong("date_firstlaunch", date_firstLaunch);
		}

		// Wait at least n days before opening
		if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
			if (System.currentTimeMillis() >= date_firstLaunch
					+ (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
				showRateDialog(mContext, editor);
			}
		}

		editor.commit();
	}

	public static void showRateDialog(final Context mContext,
			final SharedPreferences.Editor editor) {
		final Dialog dialog = new Dialog(mContext);
		// Set Dialog Title
		dialog.setTitle("Rate " + TITLE);

		LinearLayout ll = new LinearLayout(mContext);
		ll.setOrientation(LinearLayout.VERTICAL);

		TextView tv = new TextView(mContext);
		tv.setText("If you like " + TITLE
				+ ", please give us some stars and comment");
		tv.setWidth(240);
		tv.setPadding(4, 0, 4, 10);
		ll.addView(tv);

		// First Button
		Button b1 = new Button(mContext);
		b1.setText("Rate " + TITLE);
		b1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri
						.parse("market://details?id=" + PACKAGE_NAME)));
				dialog.dismiss();
			}
		});
		ll.addView(b1);

		// Second Button
		Button b2 = new Button(mContext);
		b2.setText("Remind me later");
		b2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		ll.addView(b2);

		// Third Button
		Button b3 = new Button(mContext);
		b3.setText("Stop Bugging me");
		b3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (editor != null) {
					editor.putBoolean("dontshowagain", true);
					editor.commit();
				}
				dialog.dismiss();
			}
		});
		ll.addView(b3);

		dialog.setContentView(ll);

		// Show Dialog
		dialog.show();
	}
}

This class allows you to set the preferred launch date and times before the rating the application. When the rate us launches, it shows 3 buttons and at the first button click will open a Google Play Store, and on the second button click will remind the user to rate later and the third button click will close the rate us dialog.

Next, create an XML file for your main graphical layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file activity_main.xml 

Open your activity_main.xml and paste the following code.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/rateus"/>

</RelativeLayout>

Change your application name and texts in your strings.xml and paste the following code. Go to res > values > strings.xml

strings.xml

<resources>

    <string name="app_name">RateUs Tutorial</string>
    <string name="rateus">AndroidBegin.com RateUs Tutorial</string>
    <string name="menu_settings">Settings</string>

</resources>

Finally, run the application twice for the RateUs Dialog to show up. It depends how many launches you have set in your codes. We have set two on application launches until RateUs dialog prompt. You can do modifications by changing the codes below in your RateUs.java class.

 // Day until the Rate Us Dialog Prompt Default(2 Days)
    private final static int DAYS_UNTIL_PROMPT = 0;

    // App launches until Rate Us Dialog Prompt Default(5 Launches)
    private final static int LAUNCHES_UNTIL_PROMPT = 2;

Output:

RateUs Tutorial ScreenShot

Source Code 

[purchase_link id=”7858″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

this is very useful man.. tankyu very much... but there is one problem every tim eit ask for rating rather i already rates the app... help me about this

Babul

Implementing Rate Us feature in Android

adfads

Guest

Implementing Rate Us feature in Android