Add “Rate This App” button to your Android App

Many time in out Android app, we need to open our page on Google Play Store within the app. And it is also helpful to encourage people to rate our application. So we can put a button or dialog in our app that user can use to rate our app on Google Play Store.

For that first we check that if the user device has the play store installed in their device or not. Because many device and tablets does not have play store app in their device. So we will use try catch here so if the Play Store is not available we can open the app in browser from catch part.

try {
  startActivity(new Intent(Intent.ACTION_VIEW, 
      Uri.parse("market://details?id=" + getPackageName())));
} 
catch (android.content.ActivityNotFoundException anfe) {
  startActivity(new Intent(Intent.ACTION_VIEW, 
      Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
}

Now if the device has the play store, we will directly open our app in play store app. If the play store are not found in the device it will open in browser. If you have more than one browser, it will prompt you to choose one.

startActivity(new Intent(Intent.ACTION_VIEW, 
      Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));

Here is the full code that you can use in your button onClick event.

btnRateApp.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    try {
      startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("market://details?id=" + getPackageName())));
    } 
    catch (android.content.ActivityNotFoundException anfe) {
      startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
    }
  }
});

I also make a function that you can include in your file and call it from any onClick event of button, make sure you pass the package name as argument.

public void rateApp(String packageName) {
  try {
    startActivity(new Intent(Intent.ACTION_VIEW, 
      Uri.parse("market://details?id=" + packageName)));
  } 
  catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, 
      Uri.parse("http://play.google.com/store/apps/details?id=" + packageName)));
  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s