Translating android phone number label id to string
Asked Answered
M

4

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.

Matejka answered 10/4, 2011 at 10:0 Comment(0)
A
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, "");
Alamein answered 10/4, 2011 at 16:1 Comment(5)
Yay!! You maid my day. Where did you learn about this? (always good to know for future reference)Matejka
I stumbled upon it while reading the ContactsContract documentation while doing something similar to you.Alamein
One question what is context.getResources() ?Matejka
sorry, Context is explained here: developer.android.com/reference/android/content/Context.html. If you are doing this stuff in an activity or service, use 'this' in place for context.Alamein
I got it Phone.getTypeLabel(this.getResources(), Phone.TYPE_HOME, "");Matejka
C
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.

Cuneal answered 10/4, 2011 at 10:32 Comment(1)
I said that I am doing it myself but i don't what to. To avoid languish problems.Matejka
H
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).

Hollywood answered 10/4, 2011 at 13:24 Comment(1)
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 problemMatejka
V
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));
Vtehsta answered 9/3, 2015 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.