Android How To Show Phone Contacts In AutoComplete Suggestions
|Phone Contacts Android AutoCompleteTextView
Phone Contacts Android AutoCompleteTextView. This lesson shows you how to retrieve a list of contacts whose data matches all or part of a search string, using the following techniques:
Match contact names
Retrieve a list of contacts by matching the search string to all or part of the contact name data. The Contacts Provider allows multiple instances of the same name, so this technique can return a list of matches.
Match a specific type of data, such as a phone number
Retrieve a list of contacts by matching the search string to a particular type of detail data such as an email address. For example, this technique allows you to list all of the contacts whose email address matches the search string.
Match any type of data
Retrieve a list of contacts by matching the search string to any type of detail data, including name, phone number, street address, email address, and so forth. For example, this technique allows you to accept any type of data for a search string and then list the contacts for which the data matches the string.
Note: All the example use a CursorLoader
to retrieve data from the Contacts Provider. A CursorLoader
runs its query on a thread that’s separate from the UI thread. This ensures that the query doesn’t slow down UI response times and cause a poor user experience.
Phone Contacts Android AutoCompleteTextView Output :-

Manifest Request Permission to Read the Provider
1 2 |
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_CONTACTS" /> |
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 |
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:text="Write character" android:textColor="#000000" android:layout_marginLeft="10dip"></TextView> </TableRow> <TableRow> <AutoCompleteTextView android:id="@+id/toNumber" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:textColor="#000000" android:textColorHighlight="#000000" android:textColorLink="#000000" android:textStyle="bold" android:width="250dip" /> </TableRow> </TableLayout> |
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
package com.galaxystech.autocompleteedittext; import java.util.ArrayList; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity implements OnItemClickListener, OnItemSelectedListener { // Initialize variables AutoCompleteTextView textView = null; private ArrayAdapter<String> adapter; // Store contacts values in these arraylist public static ArrayList<String> phoneValueArr = new ArrayList<String>(); public static ArrayList<String> nameValueArr = new ArrayList<String>(); EditText toNumber = null; String toNumberValue = ""; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.autocomplete_main); // Initialize AutoCompleteTextView values textView = (AutoCompleteTextView) findViewById(R.id.toNumber); //Create adapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>()); textView.setThreshold(1); //Set adapter to AutoCompleteTextView textView.setAdapter(adapter); textView.setOnItemSelectedListener(this); textView.setOnItemClickListener(this); // Read contact data and add data to ArrayAdapter // ArrayAdapter used by AutoCompleteTextView readContactData(); } private OnClickListener BtnAction(final AutoCompleteTextView toNumber) { return new OnClickListener() { public void onClick(View v) { String NameSel = ""; NameSel = toNumber.getText().toString(); final String ToNumber = toNumberValue; if (ToNumber.length() == 0) { Toast.makeText(getBaseContext(), "Please fill phone number", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getBaseContext(), NameSel + " : " + toNumberValue, Toast.LENGTH_LONG).show(); } } }; } private void readContactData() { try { /*********** Reading Contacts Name And Number **********/ String phoneNumber = ""; ContentResolver cr = getBaseContext() .getContentResolver(); //Query to get contact name Cursor cur = cr .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // If data data found in contacts if (cur.getCount() > 0) { Log.i("AutocompleteContacts", "Reading contacts........"); int k = 0; String name = ""; while (cur.moveToNext()) { String id = cur .getString(cur .getColumnIndex(ContactsContract.Contacts._ID)); name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); //Check contact have phone number if (Integer .parseInt(cur .getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { //Create query to get phone number by contact id Cursor pCur = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); int j = 0; while (pCur .moveToNext()) { // Sometimes get multiple data if (j == 0) { // Get Phone number phoneNumber = "" + pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // Add contacts names to adapter adapter.add(name); // Add ArrayList names to adapter phoneValueArr.add(phoneNumber.toString()); nameValueArr.add(name.toString()); j++; k++; } } // End while loop pCur.close(); } // End if } // End while loop } // End Cursor value check cur.close(); } catch (Exception e) { Log.i("AutocompleteContacts", "Exception : " + e); } } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub //Log.d("AutocompleteContacts", "onItemSelected() position " + position); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub InputMethodManager imm = (InputMethodManager) getSystemService( INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub // Get Array index value for selected name int i = nameValueArr.indexOf("" + arg0.getItemAtPosition(arg2)); // If name exist in name ArrayList if (i >= 0) { // Get Phone Number toNumberValue = phoneValueArr.get(i); InputMethodManager imm = (InputMethodManager) getSystemService( INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); // Show Alert Toast.makeText(getBaseContext(), "Position:" + arg2 + " Name:" + arg0.getItemAtPosition(arg2) + " Number:" + toNumberValue, Toast.LENGTH_LONG).show(); Log.d("AutocompleteContacts", "Position:" + arg2 + " Name:" + arg0.getItemAtPosition(arg2) + " Number:" + toNumberValue); } } protected void onResume() { super.onResume(); } protected void onDestroy() { super.onDestroy(); } } |
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 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.galaxystech.autocompleteedittext" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Light.NoTitleBar.Workaround" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_CONTACTS" /> </manifest> |
(544)