Eclipse/Java - Values in R.string.* return int?

42,828

Solution 1

Everything in the R class is a reference, hence it's just defined as an int.

If your code is running within — or has access to — an Android Context, you can call context.getString(R.string.my_exception_message) to get the actual String value.

Or, for things like exception strings that don't require to be translated and so don't strictly need to be defined in an Android resource .xml file, you could store the strings as constants in some sort of StringConstants interface. That way you can refer to the strings from within utility classes that may not have access to the Context.

Solution 2

Use the "getResources().getString(id)" method.

  getResources().getString(R.string.title_activity)

And boom, you have your string. Be sure to store that string in your string.xml file in the values folder.

Solution 3

Chris is right. But what if you want to localize Exception string too. For example you throw an Exception and when you catch it you want to show the exception message to the user. I would like to put them in strings.xml but at the same time I don't want to use this code

throw new Exception(context.getString(R.string.my_exception_message))

because I think that if you got an Exception probably you don't want and you can't use some extra code to get the exception message.

I think the only solution would be to declare a static String for this message in your main Activity and initialize it as soon as possible (for example in the onCreate() method) like this:

private static String my_exception_message;

public onCreate()
{
    my_exception_message = context.getString(R.string.my_exception_message);
}

So when you throw the exception you have your localized message ready to use it. What you think? You can think of some other solutions?

Share:
42,828
James Cadd
Author by

James Cadd

Updated on July 05, 2022

Comments

  • James Cadd
    James Cadd almost 2 years

    I thought I'd be classy and use the string.xml file to define some constant strings for things like exception messages. In strings.xml I hit Add, chose the "String" option (not 'String Array'), then gave it a name and value. I was surprised to see that this code doesn't work:

    throw new Exception(R.string.MyExceptionMessage);
    

    And that fails because R.string.MyExceptionMessage is actually of type int. I can verify that type by looking in R.java. What am I missing?

  • BeMeCollective
    BeMeCollective over 14 years
    Chris is right on both accounts. Context.getString loads your string resource. But beware of putting non-localizable stuff there as it will mix up with your strings for translation. I wouldn't consider it classy, but a MUST to put your UI strings in the XML files and not in the code. At a certain point you will want to distribute your apps to other countries, and to get them out of the code will require tons of effort. Do it now and save the headache. I have a distributor in China I just send her the strings.xml for translation, no effort. Regards, Ari
  • Alejandro Pablo Tkachuk
    Alejandro Pablo Tkachuk almost 7 years
    getResources() method is not always available.