android (change string in java code)

14,302

Solution 1

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

Solution 2

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:
14,302
Jack Trowbridge
Author by

Jack Trowbridge

I love mixing programming and technology with business.

Updated on June 05, 2022

Comments

  • Jack Trowbridge
    Jack Trowbridge almost 2 years

    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++;
    
            }
        });
    
  • Jack Trowbridge
    Jack Trowbridge over 12 years
    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:/
  • Will Tate
    Will Tate over 12 years
    to change the string have you just tried string = "hello world"; then display.setText(string);
  • Jack Trowbridge
    Jack Trowbridge over 12 years
    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
    Jack Trowbridge over 12 years
    are you sure that's true? you can change a xml layout at runtime:/
  • WarrenFaith
    WarrenFaith over 12 years
    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...
  • Squonk
    Squonk over 12 years
    "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.
  • Jack Trowbridge
    Jack Trowbridge over 12 years
    Sorry i get what you mean now, sorry.
  • Jack Trowbridge
    Jack Trowbridge over 12 years
    Yeah your right i have to use the setText method glad I've got you guys here to help:)
  • Jack Trowbridge
    Jack Trowbridge over 12 years
    Sorry I'm new to android, nice explaining and happy for your answer, thanks for your help.
  • Squonk
    Squonk over 12 years
    @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.