how to get the localized label of the phone types?

4.4k Views Asked by At

When changing the custom locale the label of the phone types change to the appropriate language. Does anybody know how to get the localized label of the phone types?

I pick a contact in my app to get its phone number and if there is more then one number I use an AlertDialog to let the user select the currect one. In this pick list, I want to show the label of the type, so it's easier for the user to select. Since they labels are somewhere in the Android system, it must be possible to get the localized label. Unfortunately, the Phone.LABEL is null when reading the phone number.

4

There are 4 best solutions below

2
On BEST ANSWER

I know this is a bit old, but this:

Phone.getTypeLabel(this.getResources(), cursor.getInt(typeIdx), "");

worked for me

0
On

i am using this piece of code

    public void getPhoneType(){
    int res;
    for(int i=0;i<=20;i++){
        res = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(i);
        Log.d(TAG,"i: "+ i +" type: " + context.getString(res));
    }
}

didn't found any place to get the actual count of valid types but http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html#getTypeLabelResource%28int%29 says it will always give a valid res so, you can iterate until it start giving repeated values... to me after 20 gives me the custom res.

2
On

Inferno's answer is a valid answer, and I was happy to find this answer because it was similar to what I was looking for. However, if you're dealing with phones installed with API Level 5 (Android 2.0) or newer, there is one small problem with this: android.R.array.phoneTypes only returns the list of phone types that were present prior to when ContactsContract class replaced the Contacts interface as of API Level 5. I verified the labels listed when creating a new contact on emulators running these Android versions (API Levels): 1.6 (4), 2.1-update 1 (7), and 2.2 (8).

When printed out, android.R.array.phoneTypes contains these valid phone types:
Home, Mobile, Work, Work Fax, Home Fax, Pager, Other, Custom

These are the valid phone types, present for phones with Android 2.0+ installed, that are missing from that same Array:
Callback, Car, Company Main, ISDN, Main, Other Fax, Radio, Telex, TTY TDD, Work Mobile, Work Pager, Assistant, MMS

Unfortunately, I have not been able to find something like android.R.array.phoneTypes that'll list all of these valid phone types for phones Android 2.0+. Has anyone come across such yet?

References

android.R.array.phoneTypes defined: http://developer.android.com/reference/android/R.array.html#phoneTypes

Note: I'm posting my other two reference links in separate answers, as I can't seem to post more than one hyperlink per post at this time.

1
On

Yes, you can get localized phone type string with the code:

int phoneNumberType = (int)pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), phoneNumberType , "")

but for custom phone types you should cosider phone label, not only phone type:

String phoneLabel = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));