Search code examples
androidspecial-characterscontacts

After adding contacts from code, phoneBook appends the space after period(.) in android


I don't know what I am doing wrong while inserting contact into phonebook.

here is the code to insert.

public static void AddMultipleContact(Context context, String name , String numbers, String Data4){
    ContentResolver resolver = context.getContentResolver();
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();
    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
            .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
            .withValue(ContactsContract.RawContacts.SOURCE_ID, sourceId)
            .build());
        ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, numbers) // Number of the person
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                .build()); // Type of mobile number

    android.util.Log.e(TAG, "AddMultipleContact:-------------- NAME  = " + name);
    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
            .withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,  name)
            .build());
    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
            .withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE,MIMETYPE)
            .withValue(Data.DATA1, sourceId)
            .withValue(Data.DATA3, Data4)
            .build());

    try {
        ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

As you can see i am also printing log for name.

    android.util.Log.e(TAG, "AddMultipleContact:-------------- NAME  = " + name);

Here i am sending contactName and contactNumber to this method.

the name what I am passing is suppose ex: "A.Bcdef" but the name what I am seeing in the phonebook is "A. Bcdef" It is adding extra space after period(.).

Please help me. I am not getting any solution on google, or not able to search exactly what I want to search.

Thanks in advance.


Solution

  • I SOLVED THIS BY Changing this line

    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,  name)
    

    to

    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,  name)
    

    GIVEN_NAME is taking the same name what we passed as value

    There are other parameters like DISPLAY_NAME, GIVEN_NAME, FAMILY_NAME, PREFIX, MIDDLE_NAME, SUFFIX, PHONETIC_GIVEN_NAME, PHONETIC_MIDDLE_NAME, PHONETIC_FAMILY_NAME, FULL_NAME_STYLE, PHONETIC_NAME_STYLE

    I really don't know what actually these all means.

    But when i added name as 'Testing,123' while creating a contact, then the name appears to be like '123 Testing'.

    So I thought it is automatically thinking like what is 'middleName' and 'lastName' based on SpecialCharacters.