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++;

        }
    });
share|improve this question

I've removed the references to eclipse and the eclipse tag from this question as it isn't related. – Squonk Jan 2 at 1: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 2 at 1:33
@Marcin: You get the idea - see my answer. – Squonk Jan 2 at 1:42
Yeah your right, i get it now thank for your help and time:) – Jack Trowbridge Jan 2 at 2:46
feedback

3 Answers

up vote 1 down vote accepted

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)); 
share|improve this answer
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 2 at 1: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 2 at 1:55
Sorry i get what you mean now, sorry. – Jack Trowbridge Jan 2 at 2:39
feedback

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.

share|improve this answer
but i am changing my android:text from my java file.. – Jack Trowbridge Jan 2 at 1:45
are you sure that's true? you can change a xml layout at runtime:/ – Jack Trowbridge Jan 2 at 1: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 2 at 1:55
Sorry I'm new to android, nice explaining and happy for your answer, thanks for your help. – Jack Trowbridge Jan 2 at 9:40
1  
@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 2 at 10:21
show 1 more comment
feedback

You will want to use the setText() method.

display.setText("text");
share|improve this answer
Yer i have tried "display.setText("text");" It works but only for the display variable it want to change the string variable. and "string.setText("text");" doesn't work:/ – Jack Trowbridge Jan 2 at 1:22
1  
to change the string have you just tried string = "hello world"; then display.setText(string); – William Tate Jan 2 at 1:24
Yeah your right i have to use the setText method glad I've got you guys here to help:) – Jack Trowbridge Jan 2 at 3:04
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.