28 Jan 2014

Rate Us : Rating application feature in Android

Hello friends,

Today i am going post rate your Android application.it allows you to prompt a user to rate your Android application. Ratings are determined by Google Play users. Your application’s rating is one of the most important factors influencing its ranking in the various lists and search results in Google Play. We will create an activity, and after the application launches twice the user will be prompted to rate the application. So lets start…


MainActivity.java
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // Launch RateApp.java class
  RateApp.app_launched(this);

  // Your Codes...
 }
}

//Here is the Rate app class
RateApp.java

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RateApp
{
 // Insert your Application Title
 private final static String TITLE = "My Application";

 // Insert your Application Package Name
 private final static String PACKAGE_NAME = "hb.rateapptutorial";

 // Day until the Rate Us Dialog Prompt(Default 2 Days)
 private final static int DAYS_UNTIL_PROMPT = 0;

 // Rate Us Dialog Prompt when user launch app second time
 private final static int LAUNCHES_UNTIL_PROMPT = 2;

 public static void app_launched(Context mContext)
 {
  SharedPreferences prefs = mContext.getSharedPreferences("rateapp", 0);
  if (prefs.getBoolean("dontshowagain", false))
  {
   return;
  }

  SharedPreferences.Editor editor = prefs.edit();

  // Increment launch counter
  long launch_count = prefs.getLong("launch_count", 0) + 1;
  editor.putLong("launch_count", launch_count);

  // Get date of first launch
  Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
  if (date_firstLaunch == 0)
  {
   date_firstLaunch = System.currentTimeMillis();
   editor.putLong("date_firstlaunch", date_firstLaunch);
  }

  // Wait at least n days before opening
  if (launch_count >= LAUNCHES_UNTIL_PROMPT)
  {
   if (System.currentTimeMillis() >= date_firstLaunch
     + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000))
   {
    showRateDialog(mContext, editor);
   }
  }

  editor.commit();
 }

 public static void showRateDialog(final Context mContext,final SharedPreferences.Editor editor)
 {
  final Dialog dialog = new Dialog(mContext);
  // Set Dialog Title
  dialog.setTitle("Rate " + TITLE);

  LinearLayout ll = new LinearLayout(mContext);
  ll.setOrientation(LinearLayout.VERTICAL);

  TextView tv = new TextView(mContext);
  tv.setText("If you like " + TITLE + ", please give Rating");
  tv.setWidth(240);
  tv.setPadding(4, 0, 4, 10);
  ll.addView(tv);

  // First Button
  Button btnRate = new Button(mContext);
  btnRate.setText("Rate " + TITLE);
  btnRate.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri
      .parse("market://details?id=" + PACKAGE_NAME)));
    dialog.dismiss();
   }
  });
  ll.addView(btnRate);

  // Second Button
  Button btnLater = new Button(mContext);
  btnLater.setText("Remind me later");
  btnLater.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    dialog.dismiss();
   }
  });
  ll.addView(btnLater);

  // Third Button Stop Bugging me
  Button btnNoRating = new Button(mContext);
  btnNoRating.setText("No rating");
  btnNoRating.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    if (editor != null)
    {
     editor.putBoolean("dontshowagain", true);
     editor.commit();
    }
    dialog.dismiss();
   }
  });
  ll.addView(btnNoRating);

  dialog.setContentView(ll);

  // Show Dialog
  dialog.show();
 }
}

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

2 comments:

  1. thanks...very useful program demo sir....

    ReplyDelete
  2. Sir i want to update existing Child in Expandable List View during run time.. can pls help me..

    ReplyDelete