Android Rate App Example

0

You want feedback about your app so you know what is working and what is not. You may also want the users to rate your app.

In this article, we will show you how to create a ‘Rate our App’ dialog that lets them either rate or give you a feedback.

‘Rate Our App’ Dialog Scenarios

We want the ‘Rate our App’ dialog to take care of the following choices:

  1. Rate App – On selecting this, user will be taken to app’s listing in the user’s Google Play store.
  2. Feedback – This is useful if the user is not happy with your app and would like to give you some feedback instead of opting for a negative review in app store.
  3. Later – This is useful in case user wants to rate it some other time.

Track Launch Count

Ok, now we know what choices we want to show in the Rate our App’ dialog but about the ‘when’ part, when should we show it?

User can rate your app only when the app has been tried few times which means we need to keep track of the launch count. We can either hard code the launch count or provide a setting which allows them to decide when they are ready to rate. In this example, we have hard coded the launch count to 3. The main layout shows the launch count (for the sake of example) so we know when the dialog will show up.

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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:id="@+id/launchCount" android:text="@string/app_message" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

We will store the launch count in shared preferences. Whenever user launches the app, we will increment the launch count. If the launch count is 3, we will create the ‘Rate our App’  dialog and show.

The rating dialog consists of three buttons:

  1. Positive – Rate app in play store.
    paramContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/")));

    Intent.ACTION_VIEW is the intent action you will use if you want to display some data to the user. Based on the context, it will know what to display.

  2. Negative – Open activity to send feedback. This is taken care by the special intent Intent.ACTION_SEND.
  3. Neutral – Postpone rating. This should reset the launch count.
    paramAnonymousDialogInterface.dismiss();
    AppPreferences.getInstance(MainActivity.this.getApplicationContext()).resetLaunchCount();
    

For opening the feedback dialog, we will rely on Android’s inbuilt mechanism, another special intent Intent.ACTION_SEND which is used when we want to send information to another activity.

        Intent localIntent = new Intent(Intent.ACTION_SEND);
        localIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        localIntent.putExtra(Intent.EXTRA_CC, "");
        String str = null;
        try {
            str = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0).versionName;
            localIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback for Your Android App");
            localIntent.putExtra(Intent.EXTRA_TEXT, "\n\n----------------------------------\n Device OS: Android \n Device OS version: " +
                    Build.VERSION.RELEASE + "\n App Version: " + str + "\n Device Brand: " + Build.BRAND +
                    "\n Device Model: " + Build.MODEL + "\n Device Manufacturer: " + Build.MANUFACTURER);
            localIntent.setType("message/rfc822");
            paramContext.startActivity(Intent.createChooser(localIntent, "Choose an Email client :"));
        } catch (Exception e) {
            Log.d("OpenFeedback", e.getMessage());
        }

The intent’s type message/rfc822 indicates that we want to send email. Android will display all the installed applications with a filter that matches ACTION_SEND and intent type. If more than one application matches, the system displays a disambiguation dialog (a “chooser”) that allows the user to choose an app. We have called Intent.createChooser() to pass our SEND intent. This is to make sure that a chooser is displayed even if the user has previously selected a default action for this intent.

MainActivity:

package javarticles.com.androidrateapplication;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    private TextView mlaunchCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlaunchCount = (TextView) findViewById(R.id.launchCount);
        AppPreferences.getInstance(getApplicationContext()).incrementLaunchCount();
        mlaunchCount.setText(getString(R.string.app_message,
                AppPreferences.getInstance(getApplicationContext()).getLaunchCount()));
        showRateAppDialogIfNeeded();
    }

    private void showRateAppDialogIfNeeded() {
        boolean bool = AppPreferences.getInstance(getApplicationContext()).getAppRate();
        int i = AppPreferences.getInstance(getApplicationContext()).getLaunchCount();
        if ((bool) && (i == 3)) {
            createAppRatingDialog(getString(R.string.rate_app_title), getString(R.string.rate_app_message)).show();
        }
    }

    private AlertDialog createAppRatingDialog(String rateAppTitle, String rateAppMessage) {
        AlertDialog dialog = new AlertDialog.Builder(this).setPositiveButton(getString(R.string.dialog_app_rate), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                openAppInPlayStore(MainActivity.this);
                AppPreferences.getInstance(MainActivity.this.getApplicationContext()).setAppRate(false);
            }
        }).setNegativeButton(getString(R.string.dialog_your_feedback), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                openFeedback(MainActivity.this);
                AppPreferences.getInstance(MainActivity.this.getApplicationContext()).setAppRate(false);
            }
        }).setNeutralButton(getString(R.string.dialog_ask_later), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                paramAnonymousDialogInterface.dismiss();
                AppPreferences.getInstance(MainActivity.this.getApplicationContext()).resetLaunchCount();
            }
        }).setMessage(rateAppMessage).setTitle(rateAppTitle).create();
        return dialog;
    }

    public static void openAppInPlayStore(Context paramContext) {
        paramContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/")));
    }

    public static void openFeedback(Context paramContext) {
        Intent localIntent = new Intent(Intent.ACTION_SEND);
        localIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        localIntent.putExtra(Intent.EXTRA_CC, "");
        String str = null;
        try {
            str = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0).versionName;
            localIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback for Your Android App");
            localIntent.putExtra(Intent.EXTRA_TEXT, "\n\n----------------------------------\n Device OS: Android \n Device OS version: " +
                    Build.VERSION.RELEASE + "\n App Version: " + str + "\n Device Brand: " + Build.BRAND +
                    "\n Device Model: " + Build.MODEL + "\n Device Manufacturer: " + Build.MANUFACTURER);
            localIntent.setType("message/rfc822");
            paramContext.startActivity(Intent.createChooser(localIntent, "Choose an Email client :"));
        } catch (Exception e) {
            Log.d("OpenFeedback", e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Here is preferences class to track the launch count and store the info of whether the app is already rated.

AppPreferences:

package javarticles.com.androidrateapplication;

import android.content.Context;
import android.content.SharedPreferences;


public class AppPreferences {
    private static AppPreferences sInstance;
    private SharedPreferences mPrefs;
    private static final String PREF_APP_RATE = "pref_app_rate";
    private static final String PREF_LAUNCH_COUNT = "pref_launch_count";

    private AppPreferences(Context paramContext) {
        this.mPrefs = paramContext.getSharedPreferences("app_prefs", 0);
    }

    public static AppPreferences getInstance(Context paramContext) {
        if (sInstance == null) {
            sInstance = new AppPreferences(paramContext);
        }
        return sInstance;
    }

    public boolean getAppRate() {
        return this.mPrefs.getBoolean(PREF_APP_RATE, true);
    }

    public void setAppRate(boolean paramBoolean) {
        SharedPreferences.Editor localEditor = this.mPrefs.edit();
        localEditor.putBoolean(PREF_APP_RATE, paramBoolean);
        localEditor.commit();
    }

    public int getLaunchCount() {
        return this.mPrefs.getInt(PREF_LAUNCH_COUNT, 0);
    }

    public void incrementLaunchCount() {
        int i = getLaunchCount();
        SharedPreferences.Editor localEditor = this.mPrefs.edit();
        localEditor.putInt(PREF_LAUNCH_COUNT, i + 1);
        localEditor.commit();
    }

    public void resetLaunchCount() {
        SharedPreferences.Editor localEditor = this.mPrefs.edit();
        localEditor.remove(PREF_LAUNCH_COUNT);
        localEditor.commit();
    }
}

strings.xml:

<resources>
    <string name="app_name">AndroidRateApplication</string>
    <string name="action_settings">Settings</string>
    <string name="rate_app_title">Rate our App</string>
    <string name="rate_app_message">If you like our app, please take a moment to rate it in play store!</string>
    <string name="dialog_app_rate">Rate App</string>
    <string name="dialog_ask_later">Later</string>
    <string name="dialog_your_feedback">Your Feedback</string>
    <string name="app_message">Launch count is %d</string>
</resources>

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="javarticles.com.androidrateapplication" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Run Application

Launch count

Launch Count

Launch Count

If ‘Rate App’ is chosen:

Rate Your App

Rate Your App

If feedback is chosen:

Chose Feedback

Chose Feedback

Select Gmail

Select Mail

Select Mail

Download the source code

This was an example about displaying ‘Rate our App’ dialog in Android.

You can download the source code here: AndroidRateApplication.zip
Share.

Comments are closed.