3

I would like to change the text of a TextView component to another text, that I have already created in strings.xml. When the app starts, the text shown is stored in strings.xml, under the name "help0". I would like to set this to the string under the name "help00" programmatically, by placing another "0" at the and of the name. So far I have written this code :

String help = "0";
help = help + "0";
final TextView help = (TextView) findViewById(R.id.help);
help.setText("@string/help" + help);

However, when I run the app the text changes to "@string/help00", instead of the text I have stored under help00 in strings.xml.

How could I fix this problem?

Marcus
  • 6,697
  • 11
  • 46
  • 89
levente
  • 95
  • 1
  • 1
  • 7

3 Answers3

11

Because You have concatted String resource id with normal String help So its worked as a String. You have to get first resource string from android resource and then need to concat with local String variable, like,

help.setText(getResources().getString(R.string.help)+help);

As I doubt, if you are looking for dynamically String id, You want String resource id with already exist id and '0' append to it then get int resource id from String using..

int resourceId = this.getResources().getIdentifier("@string/help" + help, "string", this.getPackageName());

and then

help.setText(resourceId);

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I wouldn't like to add a '0' to the displayed text, but to the string's ID, like I change it in the xml from android:text="@string/help0" to android:text"@string/help00". Is there a way I could do this? UPDATE: I saw your update, thank you :) – levente Feb 13 '15 at 17:01
  • i'd check for values <= 0 before calling setText. On the other hand a more clear syntax is `getIdentifier("help"+help, "string" this.getPackageName());` – Blackbelt Feb 13 '15 at 17:03
  • Something similar http://stackoverflow.com/questions/17203454/how-to-use-getresource-getidentifier-to-get-layout – user370305 Feb 13 '15 at 17:05
4

Try this

String help = "0";
final TextView tvHelp = (TextView) findViewById(R.id.help);
String yourString = getResources().getString(R.string.help);
tvHelp.setText(yourString +help)
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • My problem is, that I am changing the 'stringId', so after I change it what way could I change it to something else? (Exactly adding a '0' at the and of the stringId, so that it would become stringId0) – levente Feb 13 '15 at 16:59
  • This still only adds a '0' to the displayed text as I encountered :( – levente Feb 13 '15 at 17:12
3

I believe that strings.xml resource file content cannot be edited during runtime. now i am assuming u have the following on your strings.xml file

<resources>
    <string name="app_name">Application Name</string>
    <string name="help0">Text1</string>
    <string name="hellp00">Text2</string>
</resources>

if u want to change the text on your TextView (id=@+id/textview) from the value stored as "help0",to value stored in "help00" use the following code:

String text= getString(R.string.help00);
TextView myTextView = findViewById(R.id.textview);
myTextView.setText(text);