CBaker
CBaker

Reputation: 840

Android contact number format

I am trying to retrieve the contacts numbers in the phone and i have successfully done that, however the formatting is different per contact.

the code i use to retreive the contact information is

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

    while(cursor.moveToNext()) {

        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        System.out.println("Phone Number " + phoneNumber);

The output is

Phone Number (555) 555-5555

Phone Number 5555555555

I dont understand why this is the case. In the emulator i am using i added the contacts the exact same way. Name and a mobile number. Im not sure why they are different

Does anybody have any ideas?

Upvotes: 0

Views: 1705

Answers (2)

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

As given by @Youngjae, If you want to have formatting then you could try PhoneNumberUtils.

If you want to remove all the formatting then try String.replaceAll as given below.

number.replaceAll("[^0-9]", "");

That will remove all special characters and brackets.

Upvotes: 0

Youngjae
Youngjae

Reputation: 25080

Basically, that is because of locale settings of android you are using. I don't know exactly about yours because your emulator or app settings may be differ.

I recommend you to use PhoneNumberUtils in order to make your phone number output in same format.

And, please check this similar post also.

Upvotes: 1

Related Questions