Got To Previous Step.
- Create new Class file, named Phonebook.java.
Phonebook.java
package com.example.contactlist;
import android.net.Uri;
public class Phonebook {
String number = null,name = null;
boolean isChecked = false;
Uri imgUri = null;
public Phonebook(String name,String number,boolean selected,Uri uri){
super();
this.name = name;
this.imgUri = uri;
this.isChecked = selected;
this.number = number;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public Uri getImgUri() {
return imgUri;
}
public void setImgUri(Uri imgUri) {
this.imgUri = imgUri;
}
}
- Create another class file,named Utils.java
Utils.java
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
public class Utils {
public static String fetchContactIdFromPhoneNumber(String phoneNumber,Context context) {
// TODO Auto-generated method stub
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cFetch = context.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
String contactId = "";
if (cFetch.moveToFirst()) {
cFetch.moveToFirst();
contactId = cFetch
.getString(cFetch.getColumnIndex(PhoneLookup._ID));
}
System.out.println(contactId);
return contactId;
}
public static Uri getPhotoUri(long contactId,Context context) {
ContentResolver contentResolver = context.getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
}
- Create layout xml file in res/layout,named blacklist_layout.xml
blacklist_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:paddingTop="3dip"
android:paddingBottom="1dip" >
<ImageView
android:id="@+id/imgView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="5dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left|center"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
android:textSize="15dp"
android:textColor="#000" />
<TextView
android:id="@+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number"
android:textSize="15dp"
android:textColor="#000" />
</LinearLayout>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="right"
android:textColor="#B40404" />
</LinearLayout>
- Last but not the least, Add following code in styles.xml file.
<style name="titlebar_textview">
<item name="android:textColor">#fff</item>
<item name="android:textStyle">bold</item>
<item name="android:padding">10dp</item>
</style>
- Done ! Now Run Program.
Download Full Source Code:Download Here !
Hello! this is great! can you remake this with a contact filtering in it?
ReplyDeleteI tried adding my own custom filter.. but it doesnt work :(
ReplyDeleteGBU
ReplyDeletedane
Thank you for sharing https://www.ecomparemo.com/personal-loan
ReplyDelete