android (change string in java code)
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.
Jack Trowbridge
I love mixing programming and technology with business.
Updated on June 05, 2022Comments
-
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 over 12 yearsYer 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 over 12 yearsto change the string have you just tried
string = "hello world";
thendisplay.setText(string);
-
Jack Trowbridge over 12 yearsThank 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 over 12 yearsare you sure that's true? you can change a xml layout at runtime:/
-
WarrenFaith over 12 yearsSomehow 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 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 attributeandroid:text
is used by the layout inflater when you callsetContentView(R.layout.main)
. The layout inflater processes the XML UI elements such asTextView
. When it encounters anandroid:text
element, it callssetText(...)
passing in the text from that attribute. -
Jack Trowbridge over 12 yearsSorry i get what you mean now, sorry.
-
Jack Trowbridge over 12 yearsYeah your right i have to use the setText method glad I've got you guys here to help:)
-
Jack Trowbridge over 12 yearsSorry I'm new to android, nice explaining and happy for your answer, thanks for your help.
-
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.