Hello all,
Today I am going to share the code to fetch contact detail(name,email,pic) using Content Provider.
Overview :
To fetch email address ,name and photo,I have made a layout having a listview named :
fetch_contacts_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF00BB" >
<TextView android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#FFFFFF"
android:text="FETCH Contacts"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="18sp"
android:id="@+id/txt_header_fetchcontactlayout"/>
<ListView
android:id="@+id/list_fetchcontactlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
->Again we have to design a listview item to show single item of listview .I have named it:
contact_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:background="#F0F0F0">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout_cover">
<ImageView
android:id="@+id/icon_contactlistitem"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/icon_contactlistitem"
android:layout_marginLeft="15dp" >
<TextView
android:id="@+id/name_contactlistitem"
android:text="test"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
->Now let's move to the main activity ,I have named it FetchEmailActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
public class FetchEmailActivity extends Activity{
ListView list;
TextView txt_header;
ProgressDialog dlg;
String image_uri = "";
ArrayList<String> mImg = null,mEmial,mPhone = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fetch_contacts_layout);
txt_header = (TextView)findViewById(R.id.txt_header_fetchcontactlayout);
list = (ListView)findViewById(R.id.list_fetchcontactlayout);
txt_header.setText("Email");
LoadContacts();
}
private void LoadContacts() {
if (Build.VERSION.SDK_INT >= 11 ) {
dlg = new ProgressDialog(FetchEmailActivity.this,AlertDialog.THEME_HOLO_LIGHT );
} else {
dlg = new ProgressDialog(FetchEmailActivity.this);
}
dlg.setMessage("Loading...");
dlg.show();
mEmial = new ArrayList<String>();
mPhone = new ArrayList<String>();
mImg = new ArrayList<String>();
ContentResolver cr = FetchEmailActivity.this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
image_uri = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.PHOTO_URI));
if(email!=null){
mEmial.add(name);
mPhone.add(email);
}
if (image_uri!=null) {
if (image_uri.equals("")) {
mImg.add("no");
}
else
{
mImg.add(image_uri);
}
}
else {
mImg.add("no");
}
}
cur1.close();
}
dlg.dismiss();
list.setAdapter(null);
list.setAdapter(new SimpleContactAdapter(FetchEmailActivity.this,mEmial,mPhone,mImg));
}
}
}
->->Here is the design for SimpleContactAdapter.java The adapter which is used to show the items in listview :
import java.util.ArrayList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SimpleContactAdapter extends BaseAdapter{
private class ViewHolder {
public RelativeLayout lnCover;
public TextView tvTitle;
public ImageView img_album;
}
private ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.loading)
.cacheInMemory(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.displayer(new FadeInBitmapDisplayer(300))
.build();
private LayoutInflater mLayoutInflater = null;
private Activity activity;
private ViewHolder holder;
ArrayList<String> Showname,ShowEmail,ShowImg;
public SimpleContactAdapter(Activity activity2,
ArrayList<String> showname,ArrayList<String> email, ArrayList<String> mImg) {
this.activity = activity2;
this.Showname = showname;
this.ShowEmail = email;
this.ShowImg = mImg;
mLayoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
}
@Override
public int getCount() {
return Showname.size();
}
@Override
public Object getItem(int position) {
return Showname.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.contact_listitem, null);
holder = new ViewHolder();
holder.lnCover = (RelativeLayout)convertView.findViewById(R.id.layout_cover);
holder.tvTitle= (TextView) convertView.findViewById(R.id.name_contactlistitem);
holder.img_album = (ImageView)convertView.findViewById(R.id.icon_contactlistitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText("Name: "+Showname.get(position)+" \n Email: "+ShowEmail.get(position));
if (ShowImg.get(position).equals("no")) {
holder.img_album.setImageResource(android.R.drawable.picture_frame);
}
else {
imageLoader.displayImage(ShowImg.get(position),holder.img_album, options);
}
holder.lnCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, ShowEmail.get(position));
intent.putExtra(Intent.EXTRA_SUBJECT, "Hood Scanner Application");
activity.startActivity(Intent.createChooser(intent, "Send Email"));
}
});
return convertView;
}
}
->Here I have used Android-Universal-Image-Loader to load the image ,you can replace it with another Image Loader.
->next step will be add user-permission in Menifest file :
<uses-permission android:name="android.permission.READ_CONTACTS"/>
->That's all I have attached the screen shot below of this demo:
Today I am going to share the code to fetch contact detail(name,email,pic) using Content Provider.
Overview :
To fetch email address ,name and photo,I have made a layout having a listview named :
fetch_contacts_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF00BB" >
<TextView android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#FFFFFF"
android:text="FETCH Contacts"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="18sp"
android:id="@+id/txt_header_fetchcontactlayout"/>
<ListView
android:id="@+id/list_fetchcontactlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
->Again we have to design a listview item to show single item of listview .I have named it:
contact_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:background="#F0F0F0">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout_cover">
<ImageView
android:id="@+id/icon_contactlistitem"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/icon_contactlistitem"
android:layout_marginLeft="15dp" >
<TextView
android:id="@+id/name_contactlistitem"
android:text="test"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
->Now let's move to the main activity ,I have named it FetchEmailActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
public class FetchEmailActivity extends Activity{
ListView list;
TextView txt_header;
ProgressDialog dlg;
String image_uri = "";
ArrayList<String> mImg = null,mEmial,mPhone = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fetch_contacts_layout);
txt_header = (TextView)findViewById(R.id.txt_header_fetchcontactlayout);
list = (ListView)findViewById(R.id.list_fetchcontactlayout);
txt_header.setText("Email");
LoadContacts();
}
private void LoadContacts() {
if (Build.VERSION.SDK_INT >= 11 ) {
dlg = new ProgressDialog(FetchEmailActivity.this,AlertDialog.THEME_HOLO_LIGHT );
} else {
dlg = new ProgressDialog(FetchEmailActivity.this);
}
dlg.setMessage("Loading...");
dlg.show();
mEmial = new ArrayList<String>();
mPhone = new ArrayList<String>();
mImg = new ArrayList<String>();
ContentResolver cr = FetchEmailActivity.this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
image_uri = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.PHOTO_URI));
if(email!=null){
mEmial.add(name);
mPhone.add(email);
}
if (image_uri!=null) {
if (image_uri.equals("")) {
mImg.add("no");
}
else
{
mImg.add(image_uri);
}
}
else {
mImg.add("no");
}
}
cur1.close();
}
dlg.dismiss();
list.setAdapter(null);
list.setAdapter(new SimpleContactAdapter(FetchEmailActivity.this,mEmial,mPhone,mImg));
}
}
}
->->Here is the design for SimpleContactAdapter.java The adapter which is used to show the items in listview :
import java.util.ArrayList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SimpleContactAdapter extends BaseAdapter{
private class ViewHolder {
public RelativeLayout lnCover;
public TextView tvTitle;
public ImageView img_album;
}
private ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.loading)
.cacheInMemory(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.displayer(new FadeInBitmapDisplayer(300))
.build();
private LayoutInflater mLayoutInflater = null;
private Activity activity;
private ViewHolder holder;
ArrayList<String> Showname,ShowEmail,ShowImg;
public SimpleContactAdapter(Activity activity2,
ArrayList<String> showname,ArrayList<String> email, ArrayList<String> mImg) {
this.activity = activity2;
this.Showname = showname;
this.ShowEmail = email;
this.ShowImg = mImg;
mLayoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
}
@Override
public int getCount() {
return Showname.size();
}
@Override
public Object getItem(int position) {
return Showname.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.contact_listitem, null);
holder = new ViewHolder();
holder.lnCover = (RelativeLayout)convertView.findViewById(R.id.layout_cover);
holder.tvTitle= (TextView) convertView.findViewById(R.id.name_contactlistitem);
holder.img_album = (ImageView)convertView.findViewById(R.id.icon_contactlistitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText("Name: "+Showname.get(position)+" \n Email: "+ShowEmail.get(position));
if (ShowImg.get(position).equals("no")) {
holder.img_album.setImageResource(android.R.drawable.picture_frame);
}
else {
imageLoader.displayImage(ShowImg.get(position),holder.img_album, options);
}
holder.lnCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, ShowEmail.get(position));
intent.putExtra(Intent.EXTRA_SUBJECT, "Hood Scanner Application");
activity.startActivity(Intent.createChooser(intent, "Send Email"));
}
});
return convertView;
}
}
->Here I have used Android-Universal-Image-Loader to load the image ,you can replace it with another Image Loader.
->next step will be add user-permission in Menifest file :
<uses-permission android:name="android.permission.READ_CONTACTS"/>
->That's all I have attached the screen shot below of this demo:
No comments:
Post a Comment