-1

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?

player1ykt
  • 311
  • 1
  • 8

1 Answers1

1

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:

Richard Le Mesurier
  • 28,920
  • 20
  • 132
  • 247
  • Can't make work with string-array. code -> [link](https://i.ibb.co/ZWGYZyX/2.png) string.xml -> [link](https://i.ibb.co/GWrcY7W/3.png) – player1ykt Sep 15 '20 at 06:49
  • @player1ykt your question did not specify string arrays, check out the newly added last link that seems to give some good solutions to the problem. – Richard Le Mesurier Sep 15 '20 at 09:59