Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pic contacts name from device's contacts list in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 168
    Comment on it

    This is very important tutorial to READ EXISTING CONTACTS from device contacts list .There are some following steps to do this process.

    Step-1 - Add a permission to open and read contacts from contacts list to your manifest xml file.

     <uses-permission android:name="android.permission.READ_CONTACTS"/>
    

    Step-2- Create an Intent that ask the system to start an Activity that perform Pick action.

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
    

    Step-3- In your Activity override onActivityResult() to listen for the result respond from Step-2 code. Here is complete Activity code.

    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private static final int PICK_CONTACT = 0;
        Button button;
        TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button=(Button)findViewById(R.id.pic_contacts);
            button.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.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()) {
              @SuppressWarnings("deprecation")
                String name=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
              Log.d("Contacts Name", name);
              textView=(TextView)findViewById(R.id.contacts_name);
              textView.setText(name.toString());
              // TODO Whatever you want to do with the selected contact name.
            }
          }
          break;
        }
        }
    }
    

    Step-4- Create an XML file .

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <Button
            android:id="@+id/pic_contacts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pic Contacts" >
        </Button>
    
        <TextView
            android:id="@+id/contacts_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pic_contacts"
            android:textSize="15dp" />
    
    </RelativeLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: