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

0

3 Answers 3

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.

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

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.