15

So inside strings.xml I have a string called change_bg and I'd like to change it's value according to click events.

I know that in order to get the value you use

changeBG=getString(R.string.change_bg);

But I don't know how to SET the value of R.string.change_bg

Please let me know how.

Thanks in advance! Dvir

david
  • 1,107
  • 6
  • 12
  • 21

3 Answers3

11

You can't change resource files during runtime. Strings are hard-coded in the string.xml file and hence can't be changed during runtime. Instead of trying to edit your strings.xml file, just use SharedPreferences to store the user's preferences if that's what you're trying.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
5

You basically must understand that strings we normally hardcode, now we do it in string.xml

the strings that are variable in nature must not be defined in string.xml

You can set its default value in onCreate() of your MainActivity i.e Launcher Activity.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
3

You cannot change the values of strings.xml at run time. I had the same doubt, when I started with android development. Just remember that strings.xml can only be set before running the application manually and after that, you cannot modify it. You will understand the reason for that in the due course.

Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149