Is there an Enum string resource lookup pattern for Android?
Solution 1
You can certainly look up a resource by its name using Resources.getIdentifier()
. For instance, with the string resources you posted as an example, you can do this from an activity:
Resources res = getResources();
MyEnum e = MyEnum.VALUE1;
String localized = res.getString(res.getIdentifier(e.name(), "string", getPackageName()));
From a View, you'd have to change the last argument to getContext().getPackageName()
Solution 2
I think what you have tried is good except you dont need to pass the resources argument to the enum every time you want to translate the enum.
Use the link to subclass the Application Class, then follow this approach.
Better Solution
import android.app.Application;
public enum MyEnum {
VALUE1(R.string.VALUE1),
VALUE2(R.string.VALUE2),
.
.
VALUE10(R.string.VALUE10);
private int resourceId;
private MyEnum(int id) {
resourceId = id;
}
@Override
public String toString() {
return MyApplication.getApplicationContext().getString(resourceId);
}
}
Then calling MyEnum.VALUEx
will always give you the translated enum value, but be careful this might not be what you want always e.g you may have a raw query like this:
select * from Customer where userStatus = MyEnum.VALUEx.toString();
This may break your app, if you are storing the enum values as VALUE1, VALUE2... in db, so remember to use this MyEnum.VALUEx.name()
when you dont want to use the translated value of your MyEnum
.
select * from Customer where userStatus = MyEnum.VALUEx.name();
Solution 3
Use static Application is always a bad practice, because not only it breaks Instant Run, but also this is against the decoupling principle of programming, thus makes modularization difficult to implement. Not to mention Android actually supports multiple Applications in a single process.
For this reason, I'd suggest define an inner class for the enum to be created in runtime, whenever locale might be changed.
enum Example {
A(R.string.label_a),
B(R.string.label_b);
Example(@StringRes int label) { mLabel = label; }
private @StringRes int mLabel;
class Entry {
private Context mContext;
Entry(final Context context) { mContext = context; }
@Override public String toString() { return mContext.getString(mLabel); }
}
}
Then, build Example.Entry instance or array of Example.Entry to represent the localized version of the original enum.
Example.A.new Entry(context);
Arrays.stream(Example.values()).map(item -> item.new Entry(context)).toArray(Example.Entry[]::new)
Solution 4
If you subclass your application class, you can have it as singleton ( see:http://androidcookbook.com/Recipe.seam?recipeId=1218 ) Once you got your singleton instance , you can use it in toLocalizedString() to get resource object and get rid of parameter:
public String getString() {
return YourApp.getInstance().getResources().getString(resId);
}
voila - now you have clean looking interface.
Solution 5
enum class MeasurementEnum(var position: Int, @StringRes
var userRedableStringRes: Int) {
False(0, R.string.boolean_false),
True(1,R.string.boolean_true)
}
Related videos on Youtube
jsmith
Senior Software Engineer Specialist in C/C++ Embedded development. Experienced Android developer for app and system development.
Updated on July 09, 2022Comments
-
jsmith almost 2 years
I have an enumeration where I need to display the values as localized strings. My current approach has been this:
public enum MyEnum { VALUE1(R.string.VALUE1), VALUE2(R.string.VALUE2), . . VALUE10(R.string.VALUE10); private int mResId = -1; private MuEnum(int resId) { mResId = resId; } public String toLocalizedString(Resources r) { if (-1 != mResId) return (r.getString(mResId)); return (this.toString()); } }
Is there any easier way to to do this? I'd love it if I could somehow lookup the resource based on the enumeration value name (i.e 'VALUE1').
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="VALUE1"/>My string</string> <string name="VALUE2"/>My string 2</string> . . <string name="VALUE10"/>My string 3</string> </resources>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT: Just for future reference, this is the solution that worked best for me:
public enum MyEnum { VALUE1, VALUE2, . . VALUE10; /** * Returns a localized label used to represent this enumeration value. If no label * has been defined, then this defaults to the result of {@link Enum#name()}. * * <p>The name of the string resource for the label must match the name of the enumeration * value. For example, for enum value 'ENUM1' the resource would be defined as 'R.string.ENUM1'. * * @param context the context that the string resource of the label is in. * @return a localized label for the enum value or the result of name() */ public String getLabel(Context context) { Resources res = context.getResources(); int resId = res.getIdentifier(this.name(), "string", context.getPackageName()); if (0 != resId) { return (res.getString(resId)); } return (name()); } }
-
Oleksii K. about 9 yearsLook this post, there is much simpler solution for this question: stackoverflow.com/a/29625457/1891118
-
-
jsmith over 12 yearsIs there any reason why you wouldn't just directly inject the name of the package the enum is in instead of 'getPackageName()'? I ask because my enum is in a library shared by multiple projects.
-
Ted Hopp over 12 years@jsmith - That's precisely why you would not want to inject the package name directly. The package name that goes there is the name declared in the manifest, not the package that the enum is in.
-
jsmith over 12 yearsI looked at the generated packages within the apps that use the library, and I think I see what you mean. But, I'd love an explanation of how packaging works with inlcuded libraries. Can you reference me to documentation on this?
-
jsmith over 12 yearsMy enum is part of a library used by multiple apps. This would become tedious. I don't have a problem passing in a value.
-
Ted Hopp over 12 years@jsmith Sure. Read about library projects here. Besides the documentation for the AndroidManifest.xml file, the blog post Things That Cannot Change is well worth reading.
-
Konstantin Pribluda over 12 yearsBut your resource IDs are not portable between apps.
-
Gerhard Burger over 9 yearsInstead of
getPackageName()
you can also useBuildConfig.APPLICATION_ID
and save yourself the context trouble. -
Ted Hopp over 9 years@GerhardBurger -
BuildConfig.APPLICATION_ID
is only available for Gradle/Android Studio builds. It is not defined for other builds. -
Gerhard Burger over 9 yearsGood to know! But Android Studio seems the way to go for future projects ;)
-
Ted Hopp over 9 years@GerhardBurger - Perhaps so. I still use ADT/Eclipse. But I suspect that
BuildConfig.APPLICATION_ID
is wrong even for Gradle builds. If the code in question is part of a library, the application ID depends on what project uses the library and is not known at compile time. I suspect it will use the library's declared application name, and the wrong name will be compiled in-line into the generated class file. -
lidox almost 8 yearsWhat is "MyApplication" ? If I take my launcher activity which extends AppCompatActivity, I get following error: Non-static method "getApplicationCOntext()
-
LordWilmore about 5 yearsWhile this command may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value
-
Ted Hopp about 4 years@SiddarthG - Check for typos, including case. Make sure the resource you are trying to find is packaged in your apk. (You can check the field names in the compiler-generated
R.java
file for all the resource names packaged in your app.)