Step 1. Create Autocomplete in a xml file as i shown below
<AutoCompleteTextView android:paddingTop="15dp"
android:id="@+id/txtPhoneNo"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
Step 2. Add the following permission in your Android Mainfeast file for accessing contacts
<uses-permission android:name="android.permission.READ_CONTACTS" />
Step 3. Create an Activity and maps the autocompletetextview component in your Java Code
public class MyContacts extends Activity {
AutoCompleteTextView txtPhoneNo;
public ArrayList<String> c_Name = new ArrayList<String>();
public ArrayList<String> c_Number = new ArrayList<String>();
String[] name_Val=null;
String[] phone_Val=null;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo);
}
Step 4: Use ContentReslover to fetch the contacts, if you are running it in Android 1.6 or before version use people class for fetching contacts and add that contacts in an Array List as i shown below
ContentResolver cr1 = getContentResolver();
String[] projection = new String[] { People._ID,People.NAME,People.NUMBER };
Uri phone_contacts = People.CONTENT_URI;
Cursor managedCursor = cr1.query(phone_contacts, projection, null, null, People.NAME + " ASC");
if (managedCursor.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor.getColumnIndex(People.NAME);
int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
Log.d("int Name",Integer.toString(nameColumn));
Log.d("int Number",Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor.getString(nameColumn);
cphoneNumber = managedCursor.getString(phoneColumn);
if((contactname != " " || contactname != null) && (cphoneNumber!= " " ||cphoneNumber!= null))
{
c_Name.add(contactname);
c_Number.add(cphoneNumber);
}
} while (managedCursor.moveToNext());
}
// Donot use People class if your using android1.6 above
Uri contacts = Uri.parse("content://icc/adn");
Cursor managedCursor1 =cr.query(contacts, null, null, null, null);
if (managedCursor1.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor1.getColumnIndex("name");
int phoneColumn = managedCursor1.getColumnIndex("number");
Log.d("int Name",Integer.toString(nameColumn));
Log.d("int Number",Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor1.getString(nameColumn);
cphoneNumber = managedCursor1.getString(phoneColumn);
if((contactname != " " || contactname != null) && (cphoneNumber!= " " ||cphoneNumber!= null))
{
c_Name.add(contactname);
c_Number.add(cphoneNumber);
}
} while (managedCursor1.moveToNext());
}
Step 5: Now convert the ArrayList to Array and set that Array to AutoComplete as i Shown Below
name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
phone_Val= (String[]) c_Number.toArray(new String[c_Name.size()]); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_Val);
txtPhoneNo.setAdapter(adapter);
<AutoCompleteTextView android:paddingTop="15dp"
android:id="@+id/txtPhoneNo"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
Step 2. Add the following permission in your Android Mainfeast file for accessing contacts
<uses-permission android:name="android.permission.READ_CONTACTS" />
Step 3. Create an Activity and maps the autocompletetextview component in your Java Code
public class MyContacts extends Activity {
AutoCompleteTextView txtPhoneNo;
public ArrayList<String> c_Name = new ArrayList<String>();
public ArrayList<String> c_Number = new ArrayList<String>();
String[] name_Val=null;
String[] phone_Val=null;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo);
}
Step 4: Use ContentReslover to fetch the contacts, if you are running it in Android 1.6 or before version use people class for fetching contacts and add that contacts in an Array List as i shown below
ContentResolver cr1 = getContentResolver();
String[] projection = new String[] { People._ID,People.NAME,People.NUMBER };
Uri phone_contacts = People.CONTENT_URI;
Cursor managedCursor = cr1.query(phone_contacts, projection, null, null, People.NAME + " ASC");
if (managedCursor.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor.getColumnIndex(People.NAME);
int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
Log.d("int Name",Integer.toString(nameColumn));
Log.d("int Number",Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor.getString(nameColumn);
cphoneNumber = managedCursor.getString(phoneColumn);
if((contactname != " " || contactname != null) && (cphoneNumber!= " " ||cphoneNumber!= null))
{
c_Name.add(contactname);
c_Number.add(cphoneNumber);
}
} while (managedCursor.moveToNext());
}
// Donot use People class if your using android1.6 above
Uri contacts = Uri.parse("content://icc/adn");
Cursor managedCursor1 =cr.query(contacts, null, null, null, null);
if (managedCursor1.moveToFirst()) {
String contactname;
String cphoneNumber;
int nameColumn = managedCursor1.getColumnIndex("name");
int phoneColumn = managedCursor1.getColumnIndex("number");
Log.d("int Name",Integer.toString(nameColumn));
Log.d("int Number",Integer.toString(phoneColumn));
do {
// Get the field values
contactname = managedCursor1.getString(nameColumn);
cphoneNumber = managedCursor1.getString(phoneColumn);
if((contactname != " " || contactname != null) && (cphoneNumber!= " " ||cphoneNumber!= null))
{
c_Name.add(contactname);
c_Number.add(cphoneNumber);
}
} while (managedCursor1.moveToNext());
}
Step 5: Now convert the ArrayList to Array and set that Array to AutoComplete as i Shown Below
name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
phone_Val= (String[]) c_Number.toArray(new String[c_Name.size()]); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_Val);
txtPhoneNo.setAdapter(adapter);
good one
ReplyDelete