0

I want to implement a "Settings" section in my application, and I want that when the user selects whatever, one of the strings from string.xml will change its value according to the user selection. Is this possible at all? any ideas on how to implement it?

2 Answers2

1

You can't create strings.xml dynamically.

But you can do the following: Create a StringManager.java with a HashMap strings. Store user defined values in this map as key / value pairs. The key could be the original string from the strings.xml

public String getString(int key) { *check if string is in the map *if not in map: return the key's value, (eg.: getResources().getString(resId)), the user is happy *if key is present return the user defined value }

//call this from your Settings section setString(int key, String value) { // key is the string defined in the strings.xml, value is the user defined value. }

You also have to implement some kind of save/load methods to make changes permanent.

Whenever there is a text field that the user can change you have to set the text like this:

someTextField.setText(stringManagerObject.getString(resID));

0

strings.xml is used to store your string variables as hard-coded.

It can not be change dynamically.The purpose of strings.xml is the re-usability of variables.So , direct hard- coding in layout.xml is not good (Even it may increase a little bit speed of your application) .

If you want to implement settings in your application , go for Android Shared Preferences. Here is a good example.

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59