How to put a system contact in Edittext?

Asked

Viewed 32 times

0

I’m creating an app where the user will click a EditText which will lead to the system’s contact list, where it will click and the contact name will appear on EditText. However, it is precisely to take this contact and fill the EditText that I’m stuck. My code is this:

Comvoce3 = (EditText) findViewById(R.id.Comvoce);

    Comvoce3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentList = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intentList, RESULT_PICK);
            Comvoce3.setHint("");
        }

    });
  • Enter the code, not the code image if you want to get an answer here in the OS.

  • I put, Thank you!

1 answer

0


Try to implement the Activity result (since you are using startActivityForResult):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_PICK) {
        if (resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = {ContactsContract.Contacts.DISPLAY_NAME};
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();
            int column = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String contact = cursor.getString(column);

            Comvoce3.setHint(contact);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.