Are "compound identifiers" possible for string resources? Like this
val level = 1 <- current level
getString(R.string.answer + level) <- and this is get (R.string.answer1) string
Is something like that possible?
Are "compound identifiers" possible for string resources? Like this
val level = 1 <- current level
getString(R.string.answer + level) <- and this is get (R.string.answer1) string
Is something like that possible?
Make use of the Resources.getIdentifier(String, String, String)
method to dynamically generate the resource ID that you want, and then call getString(int)
as normal using that generated ID.
e.g.
int level = 1;
String name = "answer" + level;
int stringResId = getResources().getIdentifier(name, "string", getPackageName());
String result = getString(stringResId);
See the link for Javadoc information on the parameters.
See related duplicates for further details, including Kotlin examples: