Monday, 9 September 2013

Use dinamically R strings in Android

Use dinamically R strings in Android

I'm having a problem while using the strings stored in my strings.xml, I
have a wide list of strings stored there, they are very useful for me
because I'm using them to translate my program. However, now I want to
choose between those strings dynamically and I don't know how to do it.
Uhm, maybe it will be easy to understand with an example. Let's assume
that I have the following strings:
<string name="red">Red</string>
<string name="blue">Blue</string>
<string name="green">Green</string>
<string name="yellow">Yellow</string>
And now let's assume that I have a function that passes me a string with a
color, for example "yellow". Now I only have a solution for this, to make
a very huge switch (very very huge, because I have lots of strings), I
think that there must be an option to transform the output of my function
into the right parameter. I mean, if I have a function that returns me
"yellow", and i want to use R.strings.yellow, there must be a link
between. I don't know if maybe it could be used any kind of reflection to
achieve this.
Anyone can help me?
1,000 thanks!

No comments:

Post a Comment