September 30, 2020

Integrate In-App Review API in Android

By Mohit Agrawal

Let’s explore Google’s In-App Review API for app rating

in app review android 1

As an android developer, we always want good ratings from the users. It is because Google play store rating plays a crucial role in the app’s popularity. The question is how do you ask your user to rate the app 🙁?

The most used approach is by showing a pop-up and redirect the user to the play store. It makes a negative impact because the user has to leave the app and what is a possible chance that he will come back ?. It makes a bad user experience.

Fortunately, Google has recently announced the In-App Review API. Using this you can show the rating widget in the app itself without leaving the app.

Isn’t it awesome 😃?

Let’s learn everything about In-App review API in detail.

Implementation

Requirements and Guidelines

Before starting the actual coding part, there are some points or requirements in order to use In-App review API.

These are the requirements:

  • Android devices (phones and tablets) running Android 5.0 (API level 21) or higher that have the Google Play Store installed.
  • Chrome OS devices that have the Google Play Store installed.
  • To integrate in-app reviews in your app, your app must use version 1.8.0 or higher of the Play Core library.

You need to take care of following points:

  • Display the review card as-is, without tampering or modifying the existing design in any way, including size, opacity, shape, or other properties.
  • Do not add any overlay on top of the card or around the card.
  • Review dialog should not be programmatically removed (It will automatically be removed based on user actions).
  • To protect user privacy and avoid API misuse, this API has a limited quota per user. Hence this functionality should not be triggered on a button click.
  • Before presenting the Review card, the app should not ask user opinions like “Do you like the app?” or “Give this app 5 stars”, etc.

Let’s implement In-App review API

1 STEP: In-App review comes with the Play Core Library. Open your app level gradle file and implement the library like this.

// Play core library
implementation "com.google.android.play:core:1.8.0"
// This is to show fallback dialog alert.
implementation "com.google.android.material:material:1.3.0-alpha02"

2 STEP: We need to create an instance of the ReviewManager class. Because it provides all the required methods to start the In-App review flow.

Open your MainActivity.kt and paste the below code.

import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
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
import com.google.android.play.core.tasks.Task
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    private var reviewManager: ReviewManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }

    private fun init() {
        reviewManager = ReviewManagerFactory.create(this)
        button.setOnClickListener {
            showRateApp()
        }
    }

    fun showRateApp() {
        val request = reviewManager!!.requestReviewFlow()
        request.addOnCompleteListener { task: Task<ReviewInfo?> ->
            if (task.isSuccessful) {
                // We can get the ReviewInfo object
                val reviewInfo = task.result
                val flow =
                    reviewManager!!.launchReviewFlow(this, reviewInfo)
                flow.addOnCompleteListener { task1: Task<Void?>? -> }
            } else {
                // There was some problem, continue regardless of the result.
                // show native rate app dialog on error
                showRateAppFallbackDialog()
            }
        }
    }

    private fun showRateAppFallbackDialog() {
        MaterialAlertDialogBuilder(this)
            .setTitle("Rate us")
            .setMessage("Liked us? Please rate us on play store")
            .setPositiveButton("Okay") { dialog, which -> }
            .setNegativeButton(
                "Not now"
            ) { dialog, which -> }
            .setNeutralButton(
                "Cancel"
            ) { dialog, which -> }
            .setOnDismissListener(DialogInterface.OnDismissListener { dialog: DialogInterface? -> })
            .show()
    }
}

Let me explain the above code.

  • After creating the instance of Review Manager class, we need to call requestReviewFlow(). This will return the ReviewInfo object.
  • Now call the method launchReviewFlow() using the object of ReviewInfo.
  • We are also setting up the usual rate app dialog if the review request fails.

How to test In-App Review API

Here the implementation is easy but it becomes a little tricky when it comes to the testing.

In-app reviews require your app to be published in Play Store. However, you can test your integration without publishing your app to production using either internal test tracks or internal app sharing.

You can find more info on the testing part on the android developer page. If you have any queries, please let me know in the comments section below.

android-in-app-review-app-example 2

Conclusion

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!