This question already has answers here:
Change value of R.string programmatically
(8 answers)
Closed 6 months ago.
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
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.
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.
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.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Wanted to ask about the strings.xml in an Android project. I often see that the normal usage is to put all text for the entire App in the strings.xml. And I have also seen that strings.xml holds system strings and the rest is stored in xml files that have the name of the class. like this:
ActivityMain.java (having many text strings for user to read)
activity_main.xml (holding all text strings for ActivityMain.java)
I look trough android-best-practices but finds no mentioning of this.
Maybe this does not matter but what is your view on this, pros and cons?
To manage the strings for your project, the best approachment (From my not quite long experience) is to split the strings related to it's usage.
For example,
if you have 2 Activity: MainActivity.java with activity_main.xml (let's call them MainActivity) and SecondActivity.java with activity_second.xml, you need to split the string based on the activity name. So you need to make:
strings_main.xml
strings_second.xml
in the strings_main.xml, you need to use activityname_text pattern. For example, if in the MainActivity you have a Title TextView then you need to add:
<string name="main_title">My Title</string>
For untranslatable string in MainActivity, you can add something like this:
<string name="main_app_name" translatable="false">MyAppName</string>
<string name="main_developer_name" translatable="false">My Developer Name</string>
This approach is suitable if you have a large project where each distinct Activity (or package) is maintained by another developer. And this approach will make sure you not accidentally alter the text for other Activity than your current edited Activity.
For multiple language, we just need to make a folder based on the language. For example, for Indonesia language we just need to add values-in folder. Then we can copy the strings xml from values folder. More for Supporting Different Languages.
When adding a different language for an app (For example, Indonesia language), Android Studio will complaining about missing translation if there is a string in values but not in values-in. For example, if in values we have:
<string name="main_title">My Title</string>
But not exist in values-in then Android Studio will complaining about the non exist translation when Build-> Release the app. We can fix the error by adding translatable=false:
<string name="main_title" translatable="false">My Title</string>
We also can change the translation because there is Translation Editor in Android Studio.
For project naming convention, you can look at Ribot android-guidelines.
It all depends on your preferences. And there is really many ways of storing Strings. As many as coding styles.
One way: store all in strings.xml . This can be OK for small project,
but when quantity of strings will be over 50, all'll look messed up.
Second way: store by usage purpose. Something like login_error_strings.xml and so on. This idea is described here
Third way: store in static fields. Create some Const.class and put there all what do you want. I don't really know what is drawback of it, but there is diffidently something wrong.
Just to clarify: I think that "second way" is best way of doing this.
I'm a beginner in Android, so I hope everyone can help me to change String in strings.xml from programming.
Here is the original string:
<string name="cv_name">Hong Sengheng</string>
And I want to reset it's value to Sengheng.
<string name="cv_name">Sengheng</string>
So, hopefully everyone give me idea.
One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.
This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time.
You can not change the value of String.xml at runtime but you can change the text of that particular Ui where you pass that String...
You cann't change the value of string.xml at the runtime for this u can make a seprate class and change string value at the run time and use it
while the value of a resource should not be changed programmatically you can certainly assign a string resource to a variable String mystring = getResources().getString(R.string.mystring_res); and then change that variable's value mystring="new value";
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 :(
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.
If you need to save small amounts of String information you should be using SharedPreferences that's exactly what it's for :)
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 :)
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
I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?
You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml
Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.
Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.
can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.