Android: Find a contact by display name

In my app, I wanted to open a specific contact and add another phone number to it. I didn't have the contact id of the contact, only the display name.

Published:

Get the first contact with the name "John Johnson"

contentResolver = context.getContentResolver();
Uri uri = Data.CONTENT_URI;
String[] projection = new String[] { 
    PhoneLookup._ID 
};
String selection = 
    StructuredName.DISPLAY_NAME + " = ?";
String[] selectionArguments = { "John Johnson" };
Cursor cursor = contentResolver.query(
    uri, projection, selection, 
    selectionArguments, null);

if (cursor != null) {
    while (cursor.moveToNext()) {
        return cursor.getString(0);
    }
}
return "John Johnson not found";

Categories: Android Java

Comments