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".
Step 2
Open the Contact_picker/Manifest.xml file and update it using the following code.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.novoda"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-permission android:name="android.permission.READ_CONTACTS" />
- <uses-sdk android:minSdkVersion="3" />
- <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
- <activity android:name=".ContactSelector"
- 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>
- </manifest>
Step 3
Open the Contact_picker/res/layout/activity_main.xml file and update it using the following code.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn_contacts"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="8px"
- android:text="@string/Set_cnct" />
- <TextView
- android:id="@+id/txt_contacts_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="8px"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/txt_contacts_number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.00"
- android:textSize="24sp"/>
- </LinearLayout>
Step 4
Open the Contact_picker/res/values/string.xml file and update it using the following code.
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">Contact_picker</string>
- <string name="hello_world">Hello world!</string>
- <string name="menu_settings">Settings</string>
- <string name="Set_cnct">Import Contact</string>
- </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:
- package com.contact.picker;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.Contacts.People;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- public static final int PICK_CONTACT = 1;
- private Button btnContacts;
- private TextView txtContacts1;
- private TextView txtContacts2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnContacts = (Button) findViewById(R.id.btn_contacts);
- txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
- txtContacts2 = (TextView) findViewById(R.id.txt_contacts_number);
- btnContacts.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
- startActivityForResult(intent, PICK_CONTACT);
- }
- });
- }
- @Override
- public void onActivityResult(int reqCode, int resultCode, Intent data) {
- super.onActivityResult(reqCode, resultCode, data);
- switch (reqCode) {
- case (PICK_CONTACT):
- if (resultCode == Activity.RESULT_OK) {
- Uri contactData = data.getData();
- Cursor c = managedQuery(contactData, null, null, null, null);
- if (c.moveToFirst()) {
- String name = c.getString(c.getColumnIndexOrThrow(People.NAME))+" : c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
-
- txtContacts1.setText(name);
- }
- }
- break;
- }
- }
- }
Step 6
Run the application in an Android virtual device and see the output.
Import a Contact:
Choose a Contact from the list:
Imported Contact Name with Number: