Changing value of R.String Programmatically
Asked Answered
D

4

8

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 :(

Davena answered 6/3, 2012 at 13:53 Comment(2)
The strings in R are generated at compilation and cannot be modified as far as I know. You could instead save your strings as SharedPreferences: developer.android.com/guide/topics/data/data-storage.html#prefIlltimed
Does this answer your question? Change value of R.string programmaticallyGrizelda
F
14

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.

Fitton answered 6/3, 2012 at 13:56 Comment(0)
M
2

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

Meaty answered 6/3, 2012 at 13:55 Comment(0)
K
0

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 :)

Karrah answered 6/3, 2012 at 14:12 Comment(0)
C
0

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

Chordophone answered 17/2, 2018 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.