caps lock
caps lock

Reputation: 511

Need help to change TextView

I'm trying to change a text dynamically for a TicTacToe game.

leTexte = (TextView) findViewById(R.id.text);

I want to put on screen a TextView wich is going to change, for example if the player X won the game, "Player X won" will be displayed, or "Joueur X gagne" in French.

It's easy for me to do this manually in only one language by using

leTexte.setText("Player X Won");

But I want to use my XML files for many other languages. Here a sample of the main XML

  android:id="@+id/text"
  android:text="@string/text" 

And the english Strings.xml looks like this

    <string name="text"></string> //nothing at start
    <string name="textX">Player X Wins</string>
    <string name="textO">Player O Won !</string>
    <string name="textT">TIE !</string>
    <string name="textU">Your Turn</string>

So, how can I change the text by using the "name" ?

Upvotes: 0

Views: 45

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You can refer to strings in strings.xml as below

 tv.setText(R.string.text);

In code: Using an static integer from a sub-class of your R class, such as:

R.string.text

string is the resource type and text is the resource name.

More details at http://developer.android.com/guide/topics/resources/accessing-resources.html.

Upvotes: 3

Related Questions