7

In the /res/values folder of my android project i have a string and that is referenced in a text view in my xml file, i want to change the string in my java file.

As you can see below in the code i have made a string variable and then below that i have set what the string variable is set to, which is where the string is located. where i have "here" posed in the code that's where i want to change to string in the values folder. but i don't know what code to use to set it.

I could just change the text in a text view from my java file, which i know how to do, but that is an old way and it sets of a warning so i would rather use a string which is the best way to do so.

With my knowledge of changing text in a text view i have basically guessed my way to this stage but i don't know how to go any further could any one give me some advice on what to do, thanks.

String string;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    counter = 0;
    add = (Button) findViewById(R.id.badd);
    sub = (Button) findViewById(R.id.bsub);
    reset = (Button) findViewById(R.id.breset);
    display = (TextView) findViewById(R.id.tvdisplay);
    string = (String) getString(R.string.counter);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
               ((///////////////here////////////////))
            counter++;

        }
    });
Squonk
  • 48,735
  • 19
  • 103
  • 135
Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56
  • I've removed the references to eclipse and the eclipse tag from this question as it isn't related. – Squonk Jan 02 '12 at 01:33
  • 1
    From my understanding, those strings are meant to be constants, so I don't think you will be able to change them programmatically. I'm still new to android dev though, so don't hold me to that. – Marcin Jan 02 '12 at 01:33
  • @Marcin: You get the idea - see my answer. – Squonk Jan 02 '12 at 01:42

3 Answers3

3

You cannot modify the text assigned to <string> elements of a /res/values/strings.xml file at runtime. They're constants so effectively final.

You also cannot change a layout xml file at runtime. If you've created a layout with a TextView that has its android:text attribute set to some initial resource string, that is basically an 'initial' value and cannot be changed to something else at runtime.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • are you sure that's true? you can change a xml layout at runtime:/ – Jack Trowbridge Jan 02 '12 at 01:51
  • "but i am changing my android:text from my java file." - No you are not. You are using `setText(...)` which is a Java method and has nothing to do with the XML layout file. The attribute `android:text` is used by the layout inflater when you call `setContentView(R.layout.main)`. The layout inflater processes the XML UI elements such as `TextView`. When it encounters an `android:text` element, it calls `setText(...)` passing in the text from that attribute. – Squonk Jan 02 '12 at 01:55
  • Sorry I'm new to android, nice explaining and happy for your answer, thanks for your help. – Jack Trowbridge Jan 02 '12 at 09:40
  • 2
    @Jack: No problem. It's a tough learning curve. Do plenty of reading of examples and the Android dev docs. Also hang around here and read other peoples' questions and the answers they get - you can learn a lot that way. A year ago I was an Android rookie - I understand a great deal now but there's still a lot I haven't even started on. Stick at it. – Squonk Jan 02 '12 at 10:21
2

You told us a lot of changing text, but you don't said what the text should be. I need to guess, too:

The strings.xml file should be used for texts that might change for different languages. If you just want to change the text of a counter, you shouldn't do it via strings.xml as the numbers are universal :)

Try to go with that:

display.setText(String.valueOf(counter)); 
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Thank you, yeah i wrote i quick so wasn't up to best standard one quick question if you wouldn't mind, this works fine display.setText(String.valueOf(counter)); but would you only use that for a value or a number and you would use display.setText("") for letters or a string? – Jack Trowbridge Jan 02 '12 at 01:39
  • Somehow I don't get the problem you have. The strings.xml contains constants, so you can't change the text that is stored there. If you want to change text to a specific string that might be provided by the user, you should make it like a keyboard and have a button for each letter. Than append the letter to the string that is displayed... the stored string should only be your default starting value and can't be changed on runtime. I am a bit lost as I don't get what you really need and want... – WarrenFaith Jan 02 '12 at 01:55
  • Sorry i get what you mean now, sorry. – Jack Trowbridge Jan 02 '12 at 02:39
0

You will want to use the setText() method.

display.setText("text");
Will Tate
  • 33,439
  • 9
  • 77
  • 71