Updated 15 December 2016
In your application you may want to get any contact that is stored on user device or let user select any contact or number of contacts. This is necessary for sending message or inviting friends from an application so how we will get device contacts? It can be done by using ContactContract provider
We simply have to query ContactContract regarding the data we want. Here in this tutorial we are getting all the contacts and shows them in a list from where user can pick one or more contacts as he/she wants.
Firstly we have to add permissions to our manifest file for reading contacts
1 |
<uses-permission android:name="android.permission.READ_CONTACTS" /> |
Now create an activity/ fragment which will get contacts data and set it in a listview. Here I am using an activity, the xmls related to the activity will be like
get_contacts.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Contacts" android:textStyle="bold"/> <LinearLayout android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> </LinearLayout> </ScrollView> |
Contacts.class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Contacts{ String name; List<String> phone=new ArrayList<>(); boolean checked=false; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getPhone() { return phone; } public void setPhone(List<String> phone) { this.phone = phone; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } } |
MyContactsActivity.class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
public class MyContactsActivity extends Activity { List<Contacts> my_contacts= new ArrayList<>(); LinearLayout listLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.get_contacts); listLayout= (LinearLayout) findViewById(R.id.list); readContacts(); } public void readContacts(){ ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME); try{ if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Contacts cont =new Contacts(); cont.setName(name); List<String> ph_list= new ArrayList<>(); // get the phone number Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phone = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ph_list.add(phone); } pCur.close(); cont.setPhone(ph_list); my_contacts.add(cont); } } } cur.close(); }catch(Exception e){ e.printStackTrace(); } for(int no_of_contacts=0; no_of_contacts < my_contacts.size(); no_of_contacts++){ LinearLayout child= new LinearLayout(this); child.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); child.setOrientation(LinearLayout.HORIZONTAL); CheckBox rb= new CheckBox(this); rb.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0.2f)); rb.setTag(no_of_contacts); rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int tag = (Integer) buttonView.getTag(); if (isChecked) { my_contacts.get(tag).setChecked(true); } else { my_contacts.get(tag).setChecked(false); } } }); LinearLayout contact_info= new LinearLayout(this); contact_info.setLayoutParams(new LinearLayout.LayoutParams(0, ActionBar.LayoutParams.MATCH_PARENT,1f)); contact_info.setOrientation(LinearLayout.VERTICAL); TextView contact_name = new TextView(this); contact_name.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)); contact_name.setText(my_contacts.get(no_of_contacts).getName()); TextView phone = new TextView(this); phone.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)); phone.setText(my_contacts.get(no_of_contacts).getPhone().get(0)); TextView phone_2 = new TextView(this); phone_2.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)); phone_2.setText(my_contacts.get(no_of_contacts).getPhone().get(1)); contact_info.addView(contact_name); contact_info.addView(phone); contact_info.addView(phone_2); View line_view=new View(this); line_view.setLayoutParams(new LinearLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 1)); line_view.setBackgroundResource(R.color.black_line); child.addView(rb); child.addView(contact_info); listLayout.addView(child); listLayout.addView(line_view); } } } |
And you will get all the contacts saved on th device just like that.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.