-1

I know it's a silly quastion but when calling a string from resources(R.string.xxx), are there any ways to call an string name using variables?(for example R.string.$text).

Like:

fun mapCreator(unique:String,map:MutableMap<String,Any>,insideMap:MutableMap<Any,Any>){

    val uniqueBtn1Txt=unique+"Btn1TXT"
    insideMap[uniqueTxt]=R.string.$uniqueBtn1TXT

}
Emz
  • 9
  • 5
  • I would not do that (even though it's possible). If you need dynamic strings, you might as well format them like [how to format statement in a string resource file](https://stackoverflow.com/q/12627457/2684) – Martin Marconcini Feb 17 '22 at 08:17
  • thanks for answer but i want to call the string name with variables.i want to create a loop that adds key and values to a map and the keys are string names . and why shouldn't i do it? – Emz Feb 17 '22 at 08:55
  • There aren't specific reasons to say you shouldn't, but it would be interesting to see what the use-case is for this particular architecture. I'm sure there are valid reasons, but I'd _initially_ frown until I get a clear picture that would justify this. I'd rather `map` value X with resource Y in a testable class than a loop that dynamically constructs ids at runtime. – Martin Marconcini Feb 17 '22 at 10:29

1 Answers1

0

yes you can with this code:

val key = "some_key"
val id = context.resources.identifier(key, "string", context.packageName)
val text = context.getString(id)
Mobin Yardim
  • 695
  • 7
  • 17