Android Rate App using Google In-App Review API

Once your app is live on the play store, app ratings and reviews becomes crucial factors in driving more downloads. Typically this is done by asking users to rate the app by displaying a dialog with a couple of buttons that redirect them to the Play Store. However, this approach may increase the app's bounce rate as users may not return to the app after being redirected to play store. Additionally, novice users may find it challenging to rate the app on the play store.

Fortunately, Google has provided an API called In-App Review, which allows you to display the rating widget within the app itself, enabling users to rate the app without leaving it. The In-App Review is part of play core library. Once the widget is integrated, we can see the rating widget displayed in the same app in a bottom sheet. 



Key pointers on In-App Review API

  • In-app review works only on android devices running Android 5.0 (API level 21) or higher that have the Google Play Store installed.
  • The in-app review API is subject to quotas. The API decides how often the review widget should be shown to user. We shouldn’t call this API frequently as once user quota is reached, the widget won’t be shown to user which can break the user experience. You can read more about Quotas here.
  • The review flow will be controlled by API itself. We shouldn’t try to alter the design or place approrpiate content on top of the widget. You can read more about Design Guidelines here.
  • The review flow doesn’t indicate whether user has reviewed the app or not, or it won’t tell us whether the widget is shown to user or not.

Integrating In-App Review API

  1. To use the In-App review API, the gradle dependency has to be added to app's build.gradle first. Here I am adding material library as well as I want to show fallback rating dialog if the API throws an error.
    build.gradle
    
    // Play core library
    implementation "com.google.android.play:review-ktx:2.0.1"
     
    // optional material library to show the fallback rate us dialog
    implementation "com.google.android.material:material:1.12.0"
      
  2. The next step is creating the instance of ReviewManager interface. This class provides necessary methods to start the review flow
    • Once the new instance is created, we need to call requestReviewFlow() task which returns the ReviewInfo object upon on successful completion.
    • Using the ReviewInfo object, we need to call launchReviewFlow() method to start the review flow.
    • For some reason, if the requestReviewFlow fails, we can launch the usual Rate App dialog that redirects user to playstore app.
    • Below, showRateApp() method starts the in-app review flow. The showRateAppFallbackDialog() method acts as fallback method if requestReviewFlow throws an error. This fallback method shows usual material dialog with three buttons to redirect user to playstore app.
Here is the complete code required for in-app review flow.
MainActivity.kt

package info.androidhive.rateappapi;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.Task;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;

public class MainActivity extends AppCompatActivity {

    private ReviewManager reviewManager;

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

        init();
    }

    private void init() {
        reviewManager = ReviewManagerFactory.create(this);

        findViewById(R.id.btn_rate_app).setOnClickListener(view -> showRateApp());
    }

    /**
     * Shows rate app bottom sheet using In-App review API
     * The bottom sheet might or might not shown depending on the Quotas and limitations
     * <a href="https://developer.android.com/guide/playcore/in-app-review#quotas">...</a>
     * We show fallback dialog if there is any error
     */
    public void showRateApp() {
        Task<ReviewInfo> request = reviewManager.requestReviewFlow();
        request.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                // We can get the ReviewInfo object
                ReviewInfo reviewInfo = task.getResult();

                Task<Void> flow = reviewManager.launchReviewFlow(this, reviewInfo);
                flow.addOnCompleteListener(task1 -> {
                    // The flow has finished. The API does not indicate whether the user
                    // reviewed or not, or even whether the review dialog was shown. Thus, no
                    // matter the result, we continue our app flow.
                });
            } else {
                // There was some problem, continue regardless of the result.
                // show native rate app dialog on error
                showRateAppFallbackDialog();
            }
        });
    }

    /**
     * Showing native dialog with three buttons to review the app
     * Redirect user to PlayStore to review the app
     */
    private void showRateAppFallbackDialog() {
        new MaterialAlertDialogBuilder(this)
                .setTitle(R.string.rate_app_title)
                .setMessage(R.string.rate_app_message)
                .setPositiveButton(R.string.rate_btn_pos, (dialog, which) -> redirectToPlayStore())
                .setNegativeButton(R.string.rate_btn_neg,
                        (dialog, which) -> {
                            // take action when pressed not now
                        })
                .setNeutralButton(R.string.rate_btn_nut,
                        (dialog, which) -> {
                            // take action when pressed remind me later
                        })
                .setOnDismissListener(dialog -> {
                })
                .show();
    }

    // redirecting user to PlayStore
    public void redirectToPlayStore() {
        final String appPackageName = getPackageName();
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        } catch (ActivityNotFoundException exception) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
        }
    }
}

Testing In-App Review Flow

To test the in-app review flow, you should have the app approved already on PlayStore. This doesn’t mean the app should be available to public. You should at least have the app available for Internal Testing or Internal App Sharing.
You can find more info on testing part on android developer page. If you have any queries, please let me know in the comments section below.

Cheers!
Happy Coding 🤗
Previous Post Next Post

نموذج الاتصال