This post helps you to access contacts in android and get the selected phone number from the contacts
Step 1: Create your User Interface using layout file
For this example two controls are required, A EditText field named phoneno to display selected phone number and a Button named Contacts to launch contact picker.
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_height="wrap_content"
android:id="@+id/phoneno"
android:inputType="text|phone"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/Contacts"
android:layout_alignParentLeft="true"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Contacts"
android:text="Contacts"
android:layout_alignParentRight="true"
android:onClick="doLaunchContactPicker"></Button>
</RelativeLayout>
Step 2: doLaunchContactPicker will launch the contacts. Here's code for its implementation
import android.provider.ContactsContract.CommonDataKinds.Phone;
private static final int CONTACT_PICKER_RESULT = 1001;
Step 3: Handle results by implementing onActivityResult()
Step 1: Create your User Interface using layout file
For this example two controls are required, A EditText field named phoneno to display selected phone number and a Button named Contacts to launch contact picker.
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_height="wrap_content"
android:id="@+id/phoneno"
android:inputType="text|phone"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/Contacts"
android:layout_alignParentLeft="true"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Contacts"
android:text="Contacts"
android:layout_alignParentRight="true"
android:onClick="doLaunchContactPicker"></Button>
</RelativeLayout>
Step 2: doLaunchContactPicker will launch the contacts. Here's code for its implementation
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
[Import statements are must]
private static final int CONTACT_PICKER_RESULT = 1001;
public void doLaunchContactPicker(View view)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
Intent is used along with the startActivityForResult() method to launch another Android application and retrieve the result. Step 3: Handle results by implementing onActivityResult()
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- String phone="";
- Cursor contacts=null;
- try
- {
- if (resultCode == RESULT_OK)
- {
- switch (requestCode)
- {
- case CONTACT_PICKER_RESULT:
- // gets the uri of selected contact
- Uri result = data.getData();
- // get the contact id from the Uri (last part is contact id)
- String id = result.getLastPathSegment();
- //queries the contacts DB for phone no
- contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
- //gets index of phone no
- int phoneIdx = contacts.getColumnIndex(Phone.DATA);
- if (contacts.moveToFirst())
- {
- //gets the phone no
- phone = contacts.getString(phoneIdx);
- EditText phoneTxt=(EditText)findViewById(R.id.phoneno);
- //assigns phone no to EditText field phoneno
- phoneTxt.setText(phone);
- }
- else
- {
- Toast.makeText(this, "error", 100).show();
- }
- break;
- }
- }
- else
- {
- // gracefully handle failure
- Toast.makeText(this, "Warning: activity result not ok",50).show();
- }
- }
- catch (Exception e)
- {
- Toast.makeText(this, e.getMessage(), 50).show();
- }
- finally
- {
- if (contacts != null)
- {
- contacts.close();
- }
- }
- }
NOTE: For detailed explanation on this topic visit http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/
hello..i tried this but some error on onActivityResult line...Multiple markers at this line- void is an invalid type for the variable
ReplyDeleteonActivityResult
This method doesn't return any value. Check your function call. U would have used a variable on the left side of function call to assign the result of the function. If your not able to resolve the issue please post your code.
Delete