Introduction

In this article, I will tell you how to import a contact from the contact list in Android.
When we want to send a message, we must import contact from the contact list.
So using this article you can import contact from your contact list.
You must use the following procedure.
Step 1
Create a new project "New" --> "Android Application project" then set the name to "Contact_picker".
new.jpg

Step 2
Open the Contact_picker/Manifest.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.novoda"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-permission android:name="android.permission.READ_CONTACTS" />
  7. <uses-sdk android:minSdkVersion="3" />
  8. <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
  9. <activity android:name=".ContactSelector"
  10. android:label="@string/app_name">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>
Step 3
Open the Contact_picker/res/layout/activity_main.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:id="@+id/btn_contacts"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginLeft="8px"
  11. android:text="@string/Set_cnct" />
  12. <TextView
  13. android:id="@+id/txt_contacts_name"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="8px"
  17. android:textSize="24sp" />
  18. <TextView
  19. android:id="@+id/txt_contacts_number"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_weight="0.00"
  23. android:textSize="24sp"/>
  24. </LinearLayout>
Step 4
Open the Contact_picker/res/values/string.xml file and update it using the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Contact_picker</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="menu_settings">Settings</string>
  6. <string name="Set_cnct">Import Contact</string>
  7. </resources>
Step 5
Open Contact_picker/src/com.contact.picker/MainActivity.java and update it file using you picking contact logic as in the following:
  1. package com.contact.picker;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.Contacts.People;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. public class MainActivity extends Activity {
  13. public static final int PICK_CONTACT = 1;
  14. private Button btnContacts;
  15. private TextView txtContacts1;
  16. private TextView txtContacts2;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. btnContacts = (Button) findViewById(R.id.btn_contacts);
  22. txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
  23. txtContacts2 = (TextView) findViewById(R.id.txt_contacts_number);
  24. btnContacts.setOnClickListener(new OnClickListener() {
  25. @Override
  26. public void onClick(View arg0) {
  27. Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
  28. startActivityForResult(intent, PICK_CONTACT);
  29. }
  30. });
  31. }
  32. @Override
  33. public void onActivityResult(int reqCode, int resultCode, Intent data) {
  34. super.onActivityResult(reqCode, resultCode, data);
  35. switch (reqCode) {
  36. case (PICK_CONTACT):
  37. if (resultCode == Activity.RESULT_OK) {
  38. Uri contactData = data.getData();
  39. Cursor c = managedQuery(contactData, null, null, null, null);
  40. if (c.moveToFirst()) {
  41. String name = c.getString(c.getColumnIndexOrThrow(People.NAME))+" : c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
  42. //
  43. txtContacts1.setText(name);
  44. }
  45. }
  46. break;
  47. }
  48. }
  49. }
Step 6
Run the application in an Android virtual device and see the output.
Import a Contact:
import1.jpg
Choose a Contact from the list:
import2.jpg
Imported Contact Name with Number:
import3.jpg