0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.