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
Related
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>
I am trying to create a ListView in android but it is only generating one item on the list.
Here is my code snippet
String[] kampala1 = getResources().getStringArray(R.array.kla);
sitelistview.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_activated_1,kampala1));
i need help in getting all strings in the string array I created
string array is
<string-array name="kla">
<item>uganda Museum</item>
<item>Namugongo Museum</item>
<item>wandegeya Market</item>
<item>kasubi tombs</item>
<item>kabaka palace</item>
<item>kabaka lake</item>
</string-array>
if you want to use hard code string array then use it directly using xml.
Declare your array in strings.xml like this.
<string-array name="kla">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
and use it in your layout.
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:entries="#array/kla"/>
You could use an array Adapter. Its a good solution with ample scope for flexibility and customisation. Check the example explained in the solution below.
http://stackoverflow.com/questions/2453989/help-in-getting-string-array-from-arrays-xml-file
I am new to Android. I have a MultiSelectListPreference and I would be able to check/uncheck all the boxes, possibly through a "Select All" checkbox next to the category title, in short something like this:
This is my MultiSelectListPreference from my preference xml file:
<MultiSelectListPreference
android:title="Items list"
android:key="itemsList"
android:entries="#array/list"
android:entryValues="#array/listValues"
android:defaultValue="#array/listValues">
</MultiSelectListPreference>
Here the arrays:
<string-array name = "list">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
<string-array name = "listValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
Is there any way to implement it?
add setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) to checkbox in title.
In onCheckedChanged callback go over all items in listadapter and mark that checked
and check this answer
For example if I have the following String_Array
<string-array name="recipe_ingredients">
<item>#array/m1_ingredients</item>
<item>#array/m2_ingredients</item>
</string-array>
How can I access #array/m1_ingredients where it is another String_Array
EDIT :
No, every item of a StringArray must always be a String, of course. So it can't be another StringArray
check the Documentation on resources, scroll to StringArray
name
String. A name for the array. This name will be used as the resource ID to reference the array.
here's an eg
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
in arrays.xml
<string-array name="my_items">
<item>My item 1</item>
<item>My item 2</item>
<item>My item 3</item>
</string-array>
in strings.xml
<resources>
<string name="item1">My item 1</string>
<string name="item2">My item 2</string>
<string name="item3">My item 3</string>
</resources>
I would like to reference the string in the array "My item 1" from strings.xml. How do I do that?
oh yeah, that is what I meant. This is how I did it.
<string-array name="my_items">
<item>#string/item1</item>
<item>#string/item2</item>
<item>#string/item3</item>
</string-array>
It resolved correctly in Android 1.6
You can't. It might be possible to do the reverse: have #string/item1 in the <string-array>.