Jing Rajeev
Jing Rajeev

Reputation: 33

I want to create groups for phone contacts in android

Want to create a new group in contacts programmatically

I referred the above link but was not able to create group.

Should I create a database such that it should show only the groups which I created.

Do we have any other way in android?

Upvotes: 2

Views: 1741

Answers (1)

Rushabh Patel
Rushabh Patel

Reputation: 3080

Here is a function that works for me to create a group:

private void creategroup()
{
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation
         .newInsert(ContactsContract.Groups.CONTENT_URI)
         .withValue(ContactsContract.Groups._ID, ANY LONG UNIQUE VALUE)
         .withValue(ContactsContract.Groups.TITLE, GROUP NAME).build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } 
    catch (Exception e) {
        Log.e("Error", e.toString());
    }
}

Hope it will help you.

Upvotes: 2

Related Questions