7

Hi Im writing a small android app that is closely working whit phone labels but I don't understand how I'm suposed to translate the uri values described in the Documentation.

What i want to do is to translate TYPE_HOME To Home and so on. My current solution is to have a list of all translated string but it has presented a lot of problem with languish. But i want to be able to do it like the addresbook and other apps doses it.

KilledKenny
  • 385
  • 4
  • 19

4 Answers4

25

Android has a built in method to do this already ...

import android.provider.ContactsContract.CommonDataKinds.Phone;
String s = (String) Phone.getTypeLabel(context.getResources(), Phone.TYPE_HOME, "");
ekawas
  • 6,594
  • 3
  • 27
  • 39
1

As far as I know there're no resources for labels. You can only have all strings as resources in your own app and convert default types to these strings.

UPDATE: There're a lot of solutions for converting label type to string. For example, you can define string-array resource:

<string-array name="labels">
    <item>@string/phone</item>
    <item>@string/mobile</item>
    <item>@string/work</item>
</string-array>

Then you should define these string for all languages you support. You'll be able to convert label code to string after loading this array with getResources().getTextArray(R.array.labels). Also you have to deal with custom labels.

That's a possible solution but in fact everything depends on your app's architecture.

Michael
  • 53,859
  • 22
  • 133
  • 139
1

Looking at the source code for ContactsListActivity, it appears that the Android developers just mapped values like Phone.TYPE_MOBILE to values they defined in strings.xml, just for that app, indicating there is no universal system label to easily lookup (for example, as one might use @android:drawable to use system graphics, which is not a recommended practice, as the images change between platforms).

Thane Anthem
  • 4,093
  • 4
  • 26
  • 24
  • Hum... I had a look in the xml file but there wear no values that corresponded to `Phone.TYPE_MOBILE` or any one else. but it had call mobile and sms... You get at vote up but its not the full solution to my problem – KilledKenny Apr 10 '11 at 13:49
0

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));
Alexandr
  • 695
  • 2
  • 9
  • 18