In this post, I will show how to display Contacts in an AutoCompleteTextView. Now create a new Android Application with the following details:
Activity: activity_show_contacts.xml
<!-- LinearLayout so that dragged Components will be postioned depending upon the position of the other Components -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_ShowContactsMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ShowContactsActivity" >
<!-- The Contact's Display Names will get displayed in the AutoCompleteTextView upon typing into it. -->
<!-- Property "android:drawableRight" will show the specified icon to the Right side of the AutoCompleteTextView -->
<AutoCompleteTextView
android:id="@+id/at_Contacts"
android:layout_width="match_parent"
android:layout_height="55dp"
android:drawableRight="@drawable/ic_finder"
android:ems="10"
android:singleLine="true"
android:completionThreshold="1" >
<!-- On the Activity Start, focus will be on the AutoCompleteTextView -->
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
XML file to display Contact Name in an AutoCompleteTextView: single_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_SingleContactMain"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_margin="5dp" >
<TextView
android:id="@+id/tv_ContactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sample_text"
android:textColor="@android:color/black" />
</LinearLayout>
ShowContactsActivity.java
package com.samples.accesscontacts;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class ShowContactsActivity extends Activity {
// AutoCompleteTextView: Need to create an Object in order to Access it.
private AutoCompleteTextView mContactsAt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_contacts);
// Initialize AutoCompleteTextView object So that it can access at_Contacts
mContactsAt = (AutoCompleteTextView) findViewById(R.id.at_Contacts);
// Load the Contact Name from List into the AutoCompleteTextView
mContactsAt.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.single_contact, R.id.tv_ContactName, getAllContactNames()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_contacts, menu);
return true;
}
/**
* Get All the Contact Names
* @return
*/
private List<String> getAllContactNames() {
List<String> lContactNamesList = new ArrayList<String>();
try {
// Get all Contacts
Cursor lPeople = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (lPeople != null) {
while (lPeople.moveToNext()) {
// Add Contact's Name into the List
lContactNamesList.add(lPeople.getString(lPeople.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
}
}
} catch (NullPointerException e) {
Log.e("getAllContactNames()", e.getMessage());
}
return lContactNamesList;
}
}
Add following into the manifest file to all the application read Contacts from the phone:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Add some sample contacts in the emulator.
Run the application:
public boolean onCreateOptionsMenu(Menu menu) {
ReplyDelete// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_contacts, menu);
return true;
}
here R.menu.show_contacts gives error... plz help
Clean your project. Then try running the app. May it will help
DeleteHey Tanvi Surve.
ReplyDeletei want to Use Phone Number of Displaying Name..
i am Trying to make an Messaging App.. can you help on this?
This doesn't work for me.
ReplyDeletewhen we hit send button it is showing an error"Invalid DestinationAddress"
ReplyDelete