mlw
mlw

Reputation: 375

Need to pick contact from a dialog preference

I would like to add a preference setting that uses an ACTION_PICK intent. My goal is to acquire the phone number of a contact in my phone by using a preference. Is this possible?

I can run this code from my activity but I discovered I cannot run it from a class that extends DialogPreference.

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

Or is there a way to start a new Activity from a Preference? Then that Activity could execute the above two lines of code?

Upvotes: 0

Views: 1560

Answers (2)

Jordi B.
Jordi B.

Reputation: 1

You have to define the contact preference as "Preference ..." in preferences.xml, if you use "EditTextPreference..." the text edit dialog is shown, and this is not convenient in this case.

Upvotes: 0

Ciaran Hannigan
Ciaran Hannigan

Reputation: 21

    // Get Custom contact Pref
    Preference customContact = (Preference)findPreference("custom_contact");
    customContact.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(i, PICK_CONTACT);
            return true;
        }
    });

Upvotes: 2

Related Questions