Write data in String.xml Android - android

I want to write some values from editText of activity in the file string.xml.
I will explain with an example:
In the activity I have 2 editText and 1 button to submit the information. Imagine that the editText are for inserting NAME and SURNAME.
I have the following code in string.xml
<string name="person_name"></string>
<string name="person_surname"></string>
At the begining, those fields will be empty but after submiting the info, I want to write the values obtained from the editTexts in that file.
Any suggestion?
Thanks in advance

That's not possible. Strings.xml is only for storing predefined strings. Use SharedPreferences to store this data.

Although what you want is not possible but can be done through SharedPreferences http://developer.android.com/guide/topics/data/data-storage.html#pref

Related

How to get original text from EditText

I am new to Android, my question is how to retrieve the original value from the activitMain.xml of the android : text (without saving it to a file)
I mean that in the original value was
android : text="aaa"
then it was changed by the user to "bbb"
if i do textView.getText I get the current value (bbb) i want to retrieve aaa
Thanks
(New answer with more knowledge of question)
Maybe you can do it with Strings.xml??
<string name="player_1">Rahav</string>
<string name="player_2">Martin</string>
EditText in your xml file: android:text="#string/player_1"
Then programatically you can set it to the first player.
yourEditText.setText(getResources().getString(R.string.player_1);

Display the value '??' in strings.xml

I have a little problem but I have no answer. I use the strings.xml to store all texts from my application. If i want save the next value : '??' so I have an error when I execute my app.
<string name="unknown_text">??</string>
error: Error: No resource found that matches the given name (at 'unknown_text' with value '??').
Is there a solution to save this value in xml or I must use setText() for all widgets which need it ?
Thx
It's good habit to always quote whole string:
<string name="unknown_text">"??"</string>
use <string name="questions">\??</string>
check FormattingAndStyling and this link for more detail.

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

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?
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
strings.xml are for constant strings only. Their values can change automatically when the phone language changes provided that you have created several strings.xml files such as strings-us.xml, strings-fr.xml.
You can store username & password values in Preferences. They will be stored permanently with your application.

Which strings to store in strings.xml?

Are there any suggestions on which strings you should store in strings.xml and which strings can be stored as String objects? For example, do I have to put a string into strings.xml, if I use it only to complete a certain action and then it can be destroyed? And what is the main reason in storing strings in xml? Thanks in advance for your answers.
Any string that will be displayed to the user should be in strings.xml. This is useful in case you ever want to support other languages for your application. If you do, you just create a new strings.xml file that language with translated values. You can learn more about it here.
One reason is multi-language support.
You should store the strings that you use in Activities - TextView, button's caption and so on.
You should put most constants in strings.xml, your app title, button names, textview contents...mostly things that wont change in your application.
Another reason for storing strings in xml is for localization. You can store different files for each different Locale or language, and Android will grab the correct file for the phone's selected Locale or language.
Here is a link to the String resource Android page, it will go more deeply into how the language support is done.
You don't store all the strings in strings.xml, but only strings constants related to user interface, the strings that you want to translate in different languages.
You can have different folder like :
values
values-fr
values-de
in each a strings.xml file with you UI messages translated in many languages.
Regards,
Stéphane

Dynamically referencing a String in Strings.xml in Android

Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().

Categories

Resources