Learn how to add a rate my Application dialog on your android application.

How to add a "Rate My App" dialog to an Android Application using the Android Five Stars Library

As developers, we love to know the opinion of the users about our creations. Usually, an user won't share his oppinion publicly because of time, lazyness etc. however this is important for us, so if they won't qualify your application on the Play Store, they should at least qualify the application internally, where you can of someway know what the user thinks about your app. If you are looking for some kind of dialog that allows the user to give some stars to your apps, then you have found the right post !

In this article, we'll show you how to install and implement the Android Five Stars library in your Android Application.

1. Install Android Five Stars Library

Android Five Stars Library is a small library that helps developer add a "Rate My App" dialog to their applications. It's called "Five Stars" because the dialog has a different behaviour based on the rating given by the user. The library requires that you have the minSdkVersion set to 15 in your build.gradle file. Modify your build.gradle file and add the new library:

dependencies {
    implementation 'com.github.Angtrim:Android-Five-Stars-Library:v3.1'
}

After updating the file, synchronize your Android project. For more information about this library, please visit the official repository at Github here.

2. Implementation

After the installation, the implementation of the library in your application will be pretty easy. As first step, include the required namespaces in the class where you will execute the dialog:

// To display a message in the log (logcat)
import android.util.Log;

// Namespaces of the application
import angtrim.com.fivestarslibrary.FiveStarsDialog;
import angtrim.com.fivestarslibrary.NegativeReviewListener;
import angtrim.com.fivestarslibrary.ReviewListener;

Then, proceed to display the dialog with the following snippet:

// In this example, we are running the dialog everytime the app starts
FiveStarsDialog fiveStarsDialog = new FiveStarsDialog(this, "[email protected]");
fiveStarsDialog.setRateText("Your custom text")
    .setTitle("Your custom title")
    // If "Force Mode" is activated, when the user selects 4/5 stars
    // he is immediately redirected to the Play Store, without asking for a confirm
    .setForceMode(false)
    // Market opened if a rating >= 2 is selected
    .setUpperBound(2)
    // OVERRIDE mail intent for negative review
    .setNegativeReviewListener(this)
    // Used to listen for reviews (if you want to track them )
    .setReviewListener(this)
    // Show after defines how many times after the execution of this snippet
    // should the dialog appear (0 == immediately)
    .showAfter(0);

Note that the class that contains the code needs to implement the NegativeReviewListener and ReviewListener classes in order to use this as context of the setNegativeReviewListener and setReviewListener methods e.g:

public class MainActivity extends AppCompatActivity implements NegativeReviewListener, ReviewListener { ... }

Otherwise you will need to pass a new instance of a class that implements the mentioned classes and contains the 2 methods that work as callback for the dialog according to the user selection:

@Override
public void onNegativeReview(int stars) {
    Log.d(TAG, "Negative review " + stars);
}

@Override
public void onReview(int stars) {
    Log.d(TAG, "Review " + stars);
}

Now everytime the snippet is executed it will evaluate if it should be shown according to the value provided in the showAfter method. The library is very simple, just note that:

  • When the user tap OK or NEVER the dialog will not show again
  • When the user tap NOT NOW the access counter will be reset and the dialog will be shown again after the selected times.
  • If the user gives 4 or 5 stars out of 5, the user is sent to the Google Play Store page to give an actual rating.
  • If the user gives 3 or less stars out of 5, the user is asked to send a bug report to the developer.

Full example

The following class provides a full example of a working application that uses the library:

package com.yourcompany.yourapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import angtrim.com.fivestarslibrary.FiveStarsDialog;
import angtrim.com.fivestarslibrary.NegativeReviewListener;
import angtrim.com.fivestarslibrary.ReviewListener;


public class MainActivity extends AppCompatActivity implements NegativeReviewListener, ReviewListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // In this example, we are running the dialog everytime the app starts
        FiveStarsDialog fiveStarsDialog = new FiveStarsDialog(this,"[email protected]");
        fiveStarsDialog.setRateText("Your custom text")
            .setTitle("Your custom title")
            .setForceMode(false)
            // Market opened if a rating >= 2 is selected
            .setUpperBound(2)
            // OVERRIDE mail intent for negative review
            .setNegativeReviewListener(this)
            // Used to listen for reviews (if you want to track them )
            .setReviewListener(this)
            // Show after defines how many times after the execution of this snippet
            // should the dialog appear (0 == immediately)
            .showAfter(0);
    }

    @Override
    public void onNegativeReview(int stars) {
        Log.d(TAG, "Negative review " + stars);
    }

    @Override
    public void onReview(int stars) {
        Log.d(TAG, "Review " + stars);
    }
}

The Android Five Stars Library uses internally the android SharedPreferences class to store the number of times that the snippet to show the dialog is executed (showAfter method).

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors