Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the value of a string added in res/strings.xml on Android at run time/ or by code?

Tags:

android

I know we can use resource strings to store values, but is it possible to change those values at run time?

For example, I added two new resource elements username and password, and I want to change these values at run time. Or is there an alternate way to store values?

like image 378
BreakHead Avatar asked Nov 24 '11 14:11

BreakHead


People also ask

How do I change the value of a string xml?

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.

What is the use of string XML file in Android?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.

How do I add a new line to a string in xml?

Add \t for tab and \n for new line.

What is a string in xml?

Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one <resources> element. file location: res/values/filename.xml.


1 Answers

String resources are absolutely static defined; you can't change their values. Use SharedPreference which is to store your data, and you can change, update or do whatever to suit your needs.

Here is a sample for using SharedPreference: How to use SharedPreferences in Android to store, fetch and edit values

like image 72
Pete Houston Avatar answered Oct 21 '22 11:10

Pete Houston