-1

well i am developping my app and i want to add a contactlist in my app... i tried to understand someting but yeah... but i understood anything...

i know that i have add the permission in the manifest

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

i also know that there s an intent which i have to create... i dont know for what..

and pls dont resend this post here... i know there s some usefull code but i cant get clever of it. can someone help me with a little simple project wheres a button and when you click on the button it opens the contact menu and u can choose a contact?

How to call Android contacts list?

Community
  • 1
  • 1

1 Answers1

0

You can choose one of the contacts from PhoneBook with intent :

 private void openPhonebook()
    {
        Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
        pickContactIntent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
    }

The you should handle response intent in onActivityResult() :

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);

        if ( requestCode == PICK_CONTACT_REQUEST )
        {
            if ( resultCode == RESULT_OK )
            {
                Uri contactUri = intent.getData();
                String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };

                Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
                cursor.moveToFirst();

                int column = cursor.getColumnIndex(Phone.NUMBER);
                String phone = cursor.getString(column);

                column = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                String name = cursor.getString(column);

                //set phone and to EditText
                etName.setText(name.trim());
                petPhone.setText(phone);

                cursor.close();
            }
        }
    }

Also permission:

<uses-permission android:name="android.permission.READ_CONTACTS" />
ar-g
  • 3,417
  • 2
  • 28
  • 39