0

I'm trying to change the values in my strings.xml using Java. It's to change certain labels in an app dynamically on certain events. Any help would be appreciated

3 Answers 3

4

You can't change values in strings.xml as Strings are immutable (unless you do some dynamic compilation). You can have enums of Strings that can change the answer depending on passed-in variables

Sign up to request clarification or add additional context in comments.

2 Comments

And how would I do that? Not completely familiar with using enums
``` public class Example { public enum Foo { ONE("Bar"), TWO("Bar2"), THREE("Bar3"); String value; Foo(String value){ this.value = value; } @Override public String toString() { return this.value; } } public static void main(String[] args) { System.out.println(Foo.ONE); System.out.println(Foo.TWO); System.out.println(Foo.THREE); } } ``` EDIT: S.O. munged the code but you can copy/paste it and see what it does
1

I think the best solution for your need would be to define all possible strings within the XML File. Based on the event in your application you can then choose the correct string. If the event is something like a user input then the changing the XML file is not possible. But in this case it should be fine to change the String directly without using XML references.

Comments

0

You might want to take a look at Java String Formatter it simply let you format a string in your string.xml but not changing the whole String , as other answers said, you can't change the value of a String in strings.xml from your java code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.