android (change string in java code)
Asked Answered
P

3

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

        }
    });
Prevision answered 2/1, 2012 at 1:4 Comment(3)
I've removed the references to eclipse and the eclipse tag from this question as it isn't related.Skater
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.Melan
@Marcin: You get the idea - see my answer.Skater
A
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)); 
Alicia answered 2/1, 2012 at 1:21 Comment(3)
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?Prevision
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...Alicia
Sorry i get what you mean now, sorry.Prevision
S
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.

Skater answered 2/1, 2012 at 1:39 Comment(4)
are you sure that's true? you can change a xml layout at runtime:/Prevision
"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.Skater
Sorry I'm new to android, nice explaining and happy for your answer, thanks for your help.Prevision
@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.Skater
A
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)); 
Alicia answered 2/1, 2012 at 1:21 Comment(3)
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?Prevision
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...Alicia
Sorry i get what you mean now, sorry.Prevision
C
0

You will want to use the setText() method.

display.setText("text");
Castellanos answered 2/1, 2012 at 1:16 Comment(3)
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:/Prevision
to change the string have you just tried string = "hello world"; then display.setText(string);Castellanos
Yeah your right i have to use the setText method glad I've got you guys here to help:)Prevision

© 2022 - 2024 — McMap. All rights reserved.