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 :)

ListView - Multi Click List View : Multi Touch List View

Hello friends,

Today I am going to share very Important code for Multi-Clickable  Listview in Android.
Suppose you want perform multiple task on click of Listview like- edit, delete, share.For this I am using custom Array-adapter. Screen shot and code is given below step by step-

1. List View Clicked



2. Edit Button Clicked



3. Delete Button Clicked



activity_main.xml:

<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:background="@android:color/white"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingTop="10dp"
        android:text="Multi Touch Listview"
        android:textColor="#0099CC"
        android:textSize="20sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/textView"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="#00000000" />

</RelativeLayout>

row.xml

<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:padding="5dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="User Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="5dp"
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/darker_gray"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Edit"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/button2"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="3dp"
        android:background="@android:color/darker_gray"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Delete"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="5dp"
        android:text="Location" />

</RelativeLayout>

MainActivity.java

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity
{
 ListView userList;
 UserCustomAdapter userAdapter;
 ArrayList<User> userArray = new ArrayList<User>();

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

  //Add item in arraylist  
  userArray.add(new User("HB", "Ahmedabad", "Ahmedabad"));
  userArray.add(new User("RK", "Delhi", "Delhi"));
  userArray.add(new User("HK", "Bangure", "Bangure"));
  userArray.add(new User("AB", "Aus", "AUS"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));

  //Set value in Adapter
  userAdapter = new UserCustomAdapter(MainActivity.this, R.layout.row,userArray);
  userList = (ListView) findViewById(R.id.listView);
  userList.setItemsCanFocus(false);
  userList.setAdapter(userAdapter);

  //List click
  userList.setOnItemClickListener(new OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     final int position, long id) {
    Log.i("List View Clicked", "**********");
    Toast.makeText(MainActivity.this,
      "List View Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });

 }

}

User.xml
public class User
{
 String name;
 String address;
 String location;

 public User(String name, String address, String location)
 {
  super();
  this.name = name;
  this.address = address;
  this.location = location;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public String getLocation() {
  return location;
 }

 public void setLocation(String location) {
  this.location = location;
 } 
}

UserCustomAdapter.java

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UserCustomAdapter extends ArrayAdapter<User>
{
 Context context;
 int layoutResourceId;
 ArrayList<User> data = new ArrayList<User>();

 public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data)
 {
  super(context, layoutResourceId, data);
  this.layoutResourceId = layoutResourceId;
  this.context = context;
  this.data = data;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent)
 {
  View row = convertView;
  UserHolder holder = null;

  if (row == null)
  {
   LayoutInflater inflater = ((Activity) context).getLayoutInflater();
   row = inflater.inflate(layoutResourceId, parent, false);
   holder = new UserHolder();
   holder.textName = (TextView) row.findViewById(R.id.textView1);
   holder.textAddress = (TextView) row.findViewById(R.id.textView2);
   holder.textLocation = (TextView) row.findViewById(R.id.textView3);
   holder.btnEdit = (Button) row.findViewById(R.id.button1);
   holder.btnDelete = (Button) row.findViewById(R.id.button2);
   row.setTag(holder);
  }
  else
  {
   holder = (UserHolder) row.getTag();
  }
  User user = data.get(position);
  holder.textName.setText(user.getName());
  holder.textAddress.setText(user.getAddress());
  holder.textLocation.setText(user.getLocation());
  holder.btnEdit.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    Log.i("Edit Button Clicked", "**********");
    Toast.makeText(context, "Edit button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  holder.btnDelete.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    Log.i("Delete Button Clicked", "**********");
    Toast.makeText(context, "Delete button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  return row;

 }

 static class UserHolder
 {
  TextView textName;
  TextView textAddress;
  TextView textLocation;
  Button btnEdit;
  Button btnDelete;
 }
}

Note- Don't forget to add below line in Button View-
  android:focusable="false"
  android:focusableInTouchMode="false"

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

27 Jan 2014

Open/Read PDF file in Android

Hello Friends,

Today i am going to post pdf view sample. There are many ways to open PDF file in android application.



Solution 1 : View PDF  using external application like adobe reader.  

here i have already created on directory(pdf_Writer_Reader) in SDcard and it contain one PDF file, which i am going to open in PDFviewr application.

import java.io.File;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
 Button btnOpenPDF;
 public String Directory = Environment.getExternalStorageDirectory() + "/pdf_Writer_Reader/"; 

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

  btnOpenPDF=(Button) findViewById(R.id.btnViewPDF);

  btnOpenPDF.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    // Just Pass here PDF name which you want to open..
    OpenPDF openPDF = new OpenPDF("Impact  Brochure");
    openPDF.execute();

   }
  });
 }

 private class OpenPDF extends AsyncTask<Uri, Void, Void>
 {

  ProgressDialog pdgs = null;
  private Uri path = null;
  String filename = "";

  public OpenPDF(String filename)
  {
   this.filename = filename;

  }

  @Override
  protected void onPreExecute() {

   pdgs = ProgressDialog.show(MainActivity.this, "","Please wait...", true, false);
  }

  @Override
  protected Void doInBackground(Uri... params)
  {     

   File file=new File(Directory+filename+".pdf");
   if(file.exists())
   {
    path = Uri.fromFile(file);
    Log.e("Pdf Path=",""+path);
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void unused)
  {
   if (pdgs != null && pdgs.isShowing())
    pdgs.dismiss();

   try
   {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/pdf");
    try
    {
     startActivity(intent);

    }
    catch(ActivityNotFoundException e)
    {
     Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }


   } catch (Exception e)
   {
    e.printStackTrace();
   }
  }
 }
}

Solution 2 : Read/Write PDF  within application.  

here below is the library in which you will achieve read/write PDF file within application.
Download

Note: Do not forgot to add below  permission in android Manifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

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