How to add all predefined languages into a ListPreference dynamically? - android

I have ListPreference for selecting languages. The current trnalsations are defined in their own strings.xml as seen in the image. To fill the preference list i include the string-array elements inside the main strings.xml, making it a static configuration.
Question: Is there any way to make the Language ListPreference dynamic by iterating through all the strings.xml and extracting the Locale information from them some how on run time?
This is the current static setup:
values/strings/strings.xml
<resources>
<!-- Preferences -->
<string name="pref_language">Language</string>
<string-array name="pref_language_list">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values">
<item>en</item>
<item>se</item>
<item>de</item>
<item>fr</item>
<item>pt</item>
<item>es</item>
<item>ba</item>
</string-array>
</resources>
xml/pref_general.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:entries="#array/pref_language_list"
android:entryValues="#array/pref_language_values"
android:key="language"
android:title="#string/pref_language"
android:icon="#drawable/language"
/>
</PreferenceScreen>

entries and entryValues should work.
while I don't understand these useless string definitions & string assignments ...
add them directly into the arrays; into values/arrays.xml; with attribute translatable="false".
and these language tags also don't match the resource file locations.
<resources>
<string-array name="pref_language_list" translatable="false">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values" translatable="false">
<item>en</item>
<item>se-rSE</item>
<item>de-rDE</item>
<item>fr-rFR</item>
<item>pt-rPT</item>
<item>es-rES</item>
<item>ba-rRU</item>
</string-array>
</resources>

Related

The item in string-array can be directly localized in android?

I know I can use string resource to localize item in string-array, just like Method 1 do.
Is it OK to localize the item in string-array directly ? just like Method 2
Thanks!
--------------------------Method 1-----------------------------------------
<string-array name="Box">
<item>#string/Inbox</item>
<item>#string/Sent</item>
<item>#string/Outbox</item>
<item>#string/Draft</item>
</string-array>
<string name="Inbox">Inbox</string>
<string name="Sent">Sent</string>
<string name="Outbox">Outbox</string>
<string name="Draft">Draft</string>
<string name="Inbox">收件</string>
<string name="Sent">发件</string>
<string name="Outbox">已发</string>
<string name="Draft">草稿</string>
-----------------------------Method 2-----------------------------------------
<string-array name="Box">
<item>Inbox</item>
<item>Sent</item>
<item>Outbox</item>
<item>Draft</item>
</string-array>
<string-array name="Box">
<item>收件</item>
<item>发件</item>
<item>已发</item>
<item>草稿</item>
</string-array>
yes you can use Method2.
you just have to create localized resource folder.
here is a complete reference.

No resource found in xml

I have this error on my xml
error: Error: No resource found that matches the given name (at '^index_0' with value '#array/Tipo_peso').
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="#string/array_tipo_conversion">
<item>#array/Tipo_peso</item> //here show the error
<item>#array/Tipo_Longitud</item>
<item>#array/Tipo_Volumen</item>
</string-array>
<string-array name="#string/tipos">
<item>Peso</item>
<item>Longitud</item>
<item>Volumen</item>
</string-array>
<string-array name="#string/Tipo_peso">
<item>Kg</item>
<item>Gramos</item>
<item>Onza</item>
<item>Libras</item>
<item>Toneladas</item>
</string-array>
this is the file of strings.xml
<string name="Tipo_Volumen">Tipo_Volumen</string>
<string name="Tipo_Longitud">Tipo_Logitud</string>
<string name="Tipo_peso">Tipo_peso</string>
<string name="tipos">tipos</string>
<string name="array_tipo_conversion">array_tipo_conversion</string>
I need translated this in english and spanish
You can't use an 'identifier' as the value of attribute 'name' in the following line:
<string-array name="#string/Tipo_peso">
Consider this:
<string-array name="Tipo_peso">

Best way of getting key & value from Spinner

There are many methods for getting Key & Value from Array.
But what is best way getting Key & Value from an Array in arrays.xml like key => value via spinner?
arrays.xml:
<resources>
<string-array name="settings_listofitems">
<item name="1">item1</item>
<item name="2">item2</item>
</string-array>
</resources>
When i use code in below i get only item value. But i need item key value too:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems)[spinner1.getSelectedItemPosition()];
Any help is highly appreciated.
You can have Keys and Values both in separate Arrays, like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_listofitems_keys">
<item>1</item>
<item>2</item>
</string-array>
<string-array name="settings_listofitems_values">
<item>item1</item>
<item>item1</item>
</string-array>
</resources>
And then parse them like this :
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems_values)[spinner1.getSelectedItemPosition()];
String keyvalue = getResources().getStringArray(R.array.settings_listofitems_keys)[spinner1.getSelectedItemPosition()];
I hope this helps.

There is no Options on my list

Im trying to set up a preferences but there is no options on the list here is the code
<ListPreference
android:title="list"
android:key="list"
android:summary= "This is a list to choose from"
android:entries="#array/list"
android:entryValues="#array/lvalues"
/>
Here is the code for the array
<string-array name="list">
<item Option="1"></item>
<item Option="2"></item>
<item Option="3"></item>
<item Option="4"></item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
I want it to say Option 1 etc next to the thing
Is this what you where trying to do?
<string-array name="list">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
String Array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine string array resources with other simple resources in the one XML file, under one <resources> element.
ELEMENTS
<string-array>
Defines an array of strings. Contains one or more elements.
attributes:
name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a <string-array> element. Beware that you must escape apostrophes and quotation marks.
No attributes.
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Reference/Source:
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Why getDefaultSharedPreferences(context).getString(key, value) returns localized string?

I'm working with an application in Android and I have a list preference. The problem is that when I get the value with PreferenceManager.getDefaultSharedPreferences(context).getString(key, value) it returns the localized string instead of the entry value wich is what I'm expecting. In a previous version it was working. That is the right behavior? In that case, how shall i do to get the entry value? Here is some code to explain.
Thanks in advance.
Preferences.xml:
<ListPreference
android:id="#+id/protocol"
android:key="protocol"
android:title="#string/protocol"
android:dialogTitle="#string/change_protocol"
android:defaultValue="default"
android:entries="#array/protocol_entries"
android:entryValues="#array/protocol_entry_values" />
arrays.xml
<string-array
name="protocol_entry_values">
<item>default</item>
<item>protocol_1</item>
<item>protocol_2</item>
<item></item>
</string-array>
<string-array
name="protocol_entries">
<item>#string/label_default_protocol</item>
<item>#string/label_protocol_1</item>
<item>#string/label_protocol_2</item>
<item>#string/label_other</item>
</string-array>
strings.xml
<string name="label_default_protocol">Default protocol</string>
<string name="label_protocol_1">First protocol</string>
<string name="label_protocol_2">Second Protocol</string>
<string name="label_other">Other</string>
It sounds like you have different versions of the <string-array name="protocol_entry_values"> data in localized form. You should only have one definition of the values. The protocol_entries should be localized.

Categories

Resources