7

I need to programmatically open the android contacts app using an intent with Xamarin Forms/Android. When the Add New Contact activity/screen comes up, I would like to pre-populate it with the following fields:

  • Name (this is populating)
  • Phone (this is populating)
  • Street (Not populating)
  • City (Not populating)
  • State (Not populating)
  • Country (Not seeing a field for this, not populating)

As stated above, some screens are populating, but the address fields are not. This is the Xamarin C# Android code/service being used to trigger the activity for opening up Android's 'Add Contact' screen:

 public void AddContact(string name, string[] phoneNumbers, string streetAddress, string city, string state, string postalCode, CountryValues countrycode)
    {
        // get current activity
        var activity = CrossCurrentActivity.Current.Activity;

        // create add contact intent
        var intent = new Intent(Intent.ActionInsert);
        intent.SetType(ContactsContract.Contacts.ContentType);

        // add field for contact name
        intent.PutExtra(ContactsContract.Intents.Insert.Name, name);

        // Adding more than on phone number if available
        foreach (string numbers in phoneNumbers)
        {
            intent.PutExtra(ContactsContract.Intents.Insert.Phone, numbers);
        }

        // pre-populate address fields
        intent.PutExtra(ContactsContract.CommonDataKinds.StructuredPostal.Street, streetAddress);
        intent.PutExtra(ContactsContract.CommonDataKinds.StructuredPostal.City, city);
        intent.PutExtra(ContactsContract.CommonDataKinds.StructuredPostal.Region, state);
        intent.PutExtra(ContactsContract.CommonDataKinds.StructuredPostal.Postcode, postalCode);
        intent.PutExtra(ContactsContract.CommonDataKinds.StructuredPostal.Country, countrycode.ToString());

        //start activity
        activity.StartActivity(intent);
    }

The activity does open the 'add new contact' screen in the contacts app, but only the name and the phone number fields are populated. See screenshots below:

Populated fields

Fields not populated

I found a link that may translate into Xamarin.Android but I have been struggling with the implementation Java Samples.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hue
  • 413
  • 1
  • 3
  • 21
  • 1
    https://stackoverflow.com/questions/4459138/insert-contact-in-android-with-contactscontract – Jason Jul 20 '19 at 15:14
  • @Jason I saw that answer. However, that example inserts directly into contacts without opening Android's built in Contacts app using Activity. The point is i want to open the contacts app from my app, with the new contact details pre-populated. Which some fields are populated, but some are not. – Hue Jul 20 '19 at 15:28
  • @Jason any other suggestions would be helpful – Hue Jul 20 '19 at 16:22
  • still havent found a credible answer. I am currently looking at: http://www.javased.com/index.php?source_dir=MIT-Mobile-for-Android/src/edu/mit/mitmobile2/people/PeopleDetailActivity.java and https://gist.github.com/nolanlawson/6220089. I have tried these same codes and still no fix. – Hue Jul 22 '19 at 20:35
  • do you have setup proper permission? https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows –  Jul 22 '19 at 21:57
  • @user23333 Yes i do. Infact some contact information is already pre-populating (name, phone numbers). just cannot get city, state/region, postalcode pre-populated using intent – Hue Jul 23 '19 at 01:21
  • @user23333 I also have no issues saving the contact either. just issues pre-populating the fields i mentioned – Hue Jul 23 '19 at 01:22
  • it seems only partial fields are supported create directly; other fields need to be updated with contentresolver,the fields under [ContactsContract.Intents.Insert](https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert) are supported,may be you could use `ContactsContract.Intents.Insert.POSTAL` – Leo Zhu Jul 23 '19 at 02:03

1 Answers1

3

I'm afrain you can only pass as arguments the values available under ContactsContract.Intents.Insert. So for adress, you have only the Postal code, as detailed by the documentation:

public static final String POSTAL: added in API 5

The extra field for the contact postal address.

Type: String

Constant Value: "postal"

So you would pass the postal code as follows:

intent.PutExtra(ContactsContract.Intents.Insert.Postal, postalCode);

You may have to ask your user to input other values in the "Add Contact" page manually for now.

Also, did you test adding more than one phone at the same time the way you're doing it, with a list? From the documentation, it seems there are SECONDARY_PHONE and TERTIARY_PHONE constants you should use for that.

Mateus Wolkmer
  • 706
  • 4
  • 26
  • hey @Mateus thanks for the reply. I have updated the code to use the secondary phone and tertiary phone constants. In the Android documentation I am not seeing where it says you can only pre-populate values under the 'ContactsContract.Intents.Insert' , can you point me to that documentation please? – Hue Jul 23 '19 at 12:39
  • @Hue that comes from experience, but I've tried that quite a quile ago, so things could have changed since then, but as the only constants that worked for you were the ones in `ContactsContract.Intents.Insert` I figured that was still the case. From what I can see, `ContactsContract.CommonDataKinds` is used to retrieve data instead of inserting, as seen in https://stackoverflow.com/a/10756150/9629238 and in https://stackoverflow.com/a/29021101/9629238. – Mateus Wolkmer Jul 23 '19 at 13:15
  • I will go ahead and confirm with some rep from the Android team. If thats the case I will mark this as the answer then – Hue Jul 23 '19 at 16:02
  • @Hue Sure, I would do that too. Let me know after you get the information as well, it's good info. – Mateus Wolkmer Jul 23 '19 at 16:04
  • I did verify with the Android, team and that definitely is the case – Hue Jul 26 '19 at 16:19