SharedPreference values form xml - android

I'm writing preferences page (preferences.xml), and trying to retrieve values from resources file. This code is working;
<PreferenceScreen ...
<ListPreference
android:entries="#array/tips"
android:entryValues="#array/tips"
android:title="Tip rate"
/>
But this is not;
<EditTextPreference
android:defaultValue="#integer/invoiceNumberNext" // this doesn't work
android:key="invoice_number_next"
android:title="Next Invoice #"
android:inputType="numberDecimal"
/>
I have this in my defaults.xml page which is under res/values
<resources>
<integer name="invoiceNumberNext">1001</integer>
</resources>
Any idea why I'm not able to fetch some values?

The problem is that you misplaced your value. If you reference a value with #integer it must be in a xml file called (what a mystery...) integer.xml. This file must be in the folder structure
res/values/integer.xml
So right click on values folder and click "New-->Values resource file" and then enter integer.xml Here, now add your resources like you have done....tataaaa....you can use it.
EDIT
As I hadn´t understand the original question correctly, I have to elaborate this answer. Of course it is possible to create a file defaults.xml and put an <integer> item inside, and this is still available by #integer. So, at the beginning of this question, it seems that you misplaced the file into the wrong directory. But that isn´t the case so your problem must be anywhere else. If the default.xml is in res/values, it should work.

Related

How can I write "ó" in Android Studio on layout?

I am intented to write Iniciar sesión, but the ó not working. For this reason I changed to ó but is another error.
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="Inicio de sesión"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
¿I must configure Android Studio?
I added compileOptions.encoding "ISO-8859-1" in build.gradle but the error persist.
Help me, please.
Thank you!
To fix this issue, you have to create a Strings.xml file and add to it and qualifier (Spanish language) then you can add this letter with accent without any problem
<resources> <!-- spanish language qualifier -->
<string name="test_text">Iniciar sesión </string>
</resources>
You can read more about qualifiers in the oficial documentation here:
Qualifiers
You should avoid to use text like this, directly on your layout file. Instead of it, you should relay on the file string.xml where you define all the texts your xml files will need to display. By doing this, you will be doing nothing less than the correct specification from Android makers in order to localize your app (more info on this: https://developer.android.com/guide/topics/resources/localization.html ).
So, proceed this way:
1) on your res folder there is another one called values. There you will find your strings.xml file. Open it and create a new key, let's call it txtarriba, like this:
<string name="txtarriba">Begin of session</string>
2) with android studio or your regular file manager, create another folder below your res folder, call it values-ES.
3) inside values-ES create a new file named strings.xml, there you will insert the translation of that key txtarriba. So do this:
<string name="txtarriba">Inicio de sesión</string>
4) Then, on your primitive layout xml file you define your textview like this:
<TextView
android:id="#+id/textView"
android:layout_width="330dp"
android:layout_height="24dp"
android:text="#string/txtarriba"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="28dp" />
DONE.
With this you can assure you app is, from now on, correctly showed according to your device idiom. If your device is in english, Begin of session will be showed. If device is set with spanish language, you will read Inicio de sesión. With this you can forget all the troubles about special chars, Android manages this for you ;)

Path way from Values to XML in android studio

I'm trying to find the pathway from the XML folder in Android Studio to the Values folder which holds my list-array that I'm going to enter as the "entries" array, but I can't seem to figure it out, please help!
<ListPreference
android:title="Units"
android:defaultValue=""
android:entries=""
android:summary=""
></ListPreference>
Just give the path to the array file like this #array/whatever you named the array
eg #array/pref_units_options

Constants in Android xml file

I have some code like this in my Android settings xml file (res/xml/settings.xml):
<EditTextPreference
android:title="#string/settings_date_format"
android:summary="#string/settings_date_format_summary"
android:key="settingsDateFormat"
android:defaultValue="#string/config_settings_date_format_default_value" />
And a string like this:
<string name="config_settings_date_format_default_value">dd MMM yyyy, HH:mm</string>
As you can see, I'm setting the default value to a string. However, I'd like to have these default values stored somewhere else as it doesn't make sense to put them in res/values/strings.xml
Is it possible for me to put them in a new file called "config.xml" for example?
You can just add another XML file in the values folder and name it however you want.
On compilation time, the compiler will scan all your resources folders, regardless of the files and their names, and will create a lookup table for all your resources.
You can have multiple files with string constants in them. You just have to follow the file naming rules and make sure not to duplicate any of the string or file names.

Android set String value in String.xml

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.

Strings defined in strings.xml not displayed in "Resource chooser" in graphical layout (main.xml)

Using this tutorial at section 5.5, I attempted to "Assign the 'celsius' string attribute to the 'text' property of the first radio button and "fahrenheit" to the second"
The "Resource Chooser" (called, by mistake (?), "Reference chooser" in the above article) is displayed but...I can choose only between two strings : "app_name" and "hello". "celsius" and "fahrenheit" attributes are not proposed.
How do you solve this issue?
In the res/values folder should be an XML file called strings.xml When you open this you will see the values you referred to ("app_name" and "hello"). Make an entry similar to them like this:
<string name="celsius">Celsius</string>
You could just hard code the text into the text property you are referring to but Android provides this way to store strings of text in an XML file so that they can be 1) easily found in one spot and 2) easily translated if required later. So it is recommended to do it "the Android Way." This is explained in section 5.3 of the tutorial you linked to.
Now you should be able to reference them by the element name (celsius) as you were trying to.
The strings.xml file has to be SAVED once modified in order to have its modifications taken into account !

Categories

Resources