Reputation: 3193
Basically I need to create extract string resources and sometimes for locale there is no string resource available. Is there any way to get string or null if it is not found? I do not want to do try-catch every time it is missing
try {
valuesForLocale.put(key, res.getString(key.id));
} catch (Resources.NotFoundException e) {
//Ignore
}
Is there anything like getStringOrNull()
?
Upvotes: 0
Views: 206
Reputation: 7010
You can use getIdentifier method. The code without try-catch will look similar to:
int stringId = context.getResources().getIdentifier("my_resource_name", "string", mContext.getPackageName());
String text = (stringId != 0) ? context.getString(stringId) : "";
Upvotes: 1