Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert contacts with city, street, postal code, country in Xamarin Android using intent and activity?

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.

like image 226
Hue Avatar asked Jul 20 '19 12:07

Hue


Video Answer


1 Answers

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.

like image 145
Mateus Wolkmer Avatar answered Sep 29 '22 03:09

Mateus Wolkmer