box
box

Reputation: 4688

Android string resource with a number

Im having an issue when using getIdentifier(resourceName, "string", packageName). My string resources name is dynamic and is combined of a prefix, which is "number_" and a postfix, which is a number e.g "5". The final string name looks like this "number_5". The problem is when im using the getIdentifier method it still returns a resId, as if this resource would exists in my locale, but i don't have it in my current locale, it's in another one.

I've tried to clean my project and i tried to use getIdentifier(packageName + ":string/" + resourceName, null, null)) and also

public static int getResId(String variableName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    } 
}

The context of current locale is correct, i've checked it by:

c.getResources().getConfiguration().locale

Why does getIdentifier return a valid resource id from another non-default string resources?

Upvotes: 0

Views: 95

Answers (1)

manfcas
manfcas

Reputation: 1951

It returns a valid identifier because there is one: your R class contains the identifier. How this identifier is resolved when you load the resource is a completely different matter. Please note that you should always provide all the strings in the default folder (res/values) to avoid runtime errors.

Upvotes: 1

Related Questions