How do I call the phone's default add to contact menu?
Categories:
How to Invoke the Android 'Add to Contact' Menu Programmatically

Learn how to trigger the phone's default 'Add to Contact' menu in Android, allowing users to easily save new contact information from within your application.
Integrating with the phone's native contact management system can significantly enhance the user experience of your Android application. Instead of building a custom contact creation form, you can leverage the default 'Add to Contact' menu. This approach ensures consistency with the user's device, respects their privacy settings, and reduces development effort. This article will guide you through the process of launching this menu with pre-filled data using Android's Intent system.
Understanding Android Intents for Contacts
Android's Intent system is a powerful mechanism for inter-component communication. To interact with the Contacts application, we'll use specific Intent actions and data types. The primary action for adding a new contact is Intent.ACTION_INSERT, combined with the ContactsContract.Contacts.CONTENT_ITEM_TYPE data type. This tells the Android system that we want to insert a new item of the 'contact' type.
flowchart TD
A[Your App] --> B{Create Intent}
B --> C{Set Action: ACTION_INSERT}
C --> D{Set Data Type: Contacts.CONTENT_ITEM_TYPE}
D --> E{Add Extras: Name, Phone, Email}
E --> F{Start Activity: startActivity(intent)}
F --> G[Android Contacts App]
G --> H[User Saves/Cancels Contact]
H --> I[Return to Your App (Optional)]Flowchart of invoking the 'Add to Contact' menu
Implementing the 'Add to Contact' Intent
To launch the 'Add to Contact' menu, you need to create an Intent and populate it with the necessary data. You can pre-fill fields like name, phone number, and email address using Intent extras. The ContactsContract.Intents.Insert class provides convenient constants for these extra keys.
import android.content.Intent;
import android.provider.ContactsContract;
import android.net.Uri;
public class ContactHelper {
public static void addContact(android.content.Context context, String name, String phone, String email) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (name != null && !name.isEmpty()) {
intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
}
if (phone != null && !phone.isEmpty()) {
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
}
if (email != null && !email.isEmpty()) {
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
}
// Ensure there's an app to handle the intent
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} else {
// Handle the case where no app can handle the intent
// e.g., show a Toast message to the user
android.widget.Toast.makeText(context, "No application found to add contacts.", android.widget.Toast.LENGTH_SHORT).show();
}
}
// Example usage:
// ContactHelper.addContact(this, "John Doe", "+1234567890", "john.doe@example.com");
}
Java code to create and launch an Intent for adding a contact
Intent using intent.resolveActivity(context.getPackageManager()) != null. This prevents your app from crashing if the user's device doesn't have a contacts app or if it's disabled.Handling Permissions
Unlike reading contacts, adding a contact using ACTION_INSERT does not require explicit runtime permissions in your AndroidManifest.xml or at runtime. The Contacts application itself handles the permission to write to the contacts database. Your application is simply delegating the task to the system app.
ACTION_INSERT for contacts doesn't require WRITE_CONTACTS permission for your app, if you were to directly manipulate the ContactsContract content provider, you would need it. Using ACTION_INSERT is the recommended and safer approach for user-initiated contact additions.1. Prepare Contact Data
Gather the contact information (name, phone, email) that you wish to pre-fill in the 'Add to Contact' form. These can come from user input, network requests, or other parts of your application.
2. Create the Intent
Instantiate a new Intent with Intent.ACTION_INSERT and set its type to ContactsContract.Contacts.CONTENT_TYPE.
3. Add Extras
Use intent.putExtra() with keys from ContactsContract.Intents.Insert to add the prepared contact data. Only add extras for data you have.
4. Verify and Launch
Before calling startActivity(intent), verify that an activity exists to handle the intent using intent.resolveActivity(). If an activity is found, launch it; otherwise, inform the user.