Android set String value in String.xml - android

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.

Related

How to reset string value from java in Android?

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";

Android: Organizing Strings and String Arrays in res/values

I am working on a project that includes a lot of strings and string arrays. I would like to put them into created folders inside res/values, but I get errors when I try to do this. Either getRecources() does not recognize the new folder or the xml attributes cannot link together. I know this is a noob-ish question, but thanks for the help!
Unfortunately, you can't create any subfolders in your values folder. But you have two instruments to control the hierarchy.
String arrays are declared in the following way:
<string-array name="arr_name">
<item>Text</item>
<item>Another text</item>
</string-array>
You can access them through R.array.arr_name.
Prefixes are kind of obvious, but since you mentioned that you are a novice, it's worth mentioning. I usually prefix all of my strings depending on how they are used. For example, btn_ for the text used on buttons, dialog_ for strings used in dialogs and so on. This way autocomplete in the IDE also works much better too.
Also you can split your declarations into different files, but this doesn't have any impact at all on the way you access them, so I don't know if this can help you.
You can define array of strings using following way. Later you can access it in code with R.values.langs
<string-array name="langs">
<item>бг</item>
<item>en</item>
<item>ру</item>
</string-array>
To organise my res folder I use defined xml files not sub-folders.
Basic Example:
- if you have Strings for your Login Page put them in login_strings.xml
- if you have Strings for your Options Page put them in options_strings.xml
etc.
Hope this helps.

Storing strings in android XML resource file and referencing them

I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));

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

Android Strings

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

Categories

Resources