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:
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:
I found a link that may translate into Xamarin.Android but I have been struggling with the implementation Java Samples.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With