Falcon165o
Falcon165o

Reputation: 2965

Changing value of R.String Programmatically

Can you change the values of a R.string programmatically in an android program? I need to pull some API information (for example battery state, battery percentage, android os version) and would like to save it a R.string value. I know how to read it:

 String helloValue= getResources().getString(R.string.hello);

I've also looked at: Change value of R.string programically? but it seems that only involves changing language and he ended up doing it a different way. Can anyone lend a hand please? I've also looked here: http://developer.android.com/guide/topics/resources/string-resource.html and found nothing to help out either :(

Upvotes: 8

Views: 34823

Answers (4)

Abdul Qadir
Abdul Qadir

Reputation: 671

You can get value from res/string value by this way.

String str = getResources().getString(R.string.<name_of_value>);

Now you can further use this value of this(str) String variable.
But according to me there is no way to set value of res/string

Upvotes: 0

hqt
hqt

Reputation: 30304

There are many ways to finish your ideas. But, you cannot change your String resource, (as well as R.java resource), or layout resource,... and any other resource. You can do this by change resource to assets. this can read and write :)

Upvotes: 0

Egor
Egor

Reputation: 40228

You can't change strings.xml dynamically since it's a compiled resource. There are other mechanisms for saving data in Android, here's a nice post that covers this topic: Data Storage. Hope this helps.

Upvotes: 14

ScouseChris
ScouseChris

Reputation: 4397

If you need to save small amounts of String information you should be using SharedPreferences that's exactly what it's for :)

Upvotes: 2

Related Questions