Implementing “Rate This App” Feature

If you have already published your app into android market, you might want to get a feedback from your users. This can be done in Google Play where users can rate an app and leave a comment. It will help you as a developer to improve your app and allows other users to see if your app is worth installing or not. Also, it might improve the criteria or your app on search result pages of Google Play.

To get more user rating and feeding the best way is to add a “Rate this app” button in your app. To proceed on this first thing you will need is your id in Google Play. To find out the id search your app in Google Play and check the url.

For example:

 

https://play.google.com/store/apps/details?id=com.supercell.hayda

 

If this is the URL of your app, then the id is com.supercell.hayda. Note this id because we will need it in our code.(example url courtesy supercell) Now let’s start with the code to implement rate this app feature. Follow the steps below:

Step1: Create a new Android project in your android IDE.

Step2: Get the ID of your app from android market or google play.

Step3: Write following into your main layout file:

<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:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate This App"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp" />

</RelativeLayout>

Step4: Write following into your main activity file:

package com.example.ratethisapp;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

Button btnRate = (Button) findViewById(R.id.button);
btnRate.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
onClickRateThisApp(v);
}});
}

private boolean isActivityStarted(Intent aIntent) {
try
{
startActivity(aIntent);
return true;
}
catch (ActivityNotFoundException e)
{
return false;
}
}

public void onClickRateThisApp(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
@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);
}
}

 

Step5: Run for the output:

rate1

rate2

rate3

Checkout my previous blog on Camera Overlay Preview

Leave a Comment: