Android MultiSelectListPreference with "Select All" checkbox in the title - android

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

Related

How can I make code responsive with any language? Support multiple languages

strings (English is the default)
<string-array name="languagesList">
<item>English</item>
<item>Arabic</item>
<item>Spanish</item>
<item>French</item>
<item>Chinese</item>
</string-array>
strings-ar
<string-array name="languagesList">
<item>الإنجليزية</item>
<item>العربية</item>
<item>الإسبانية</item>
<item>الفرنسية</item>
<item>الصينية</item>
</string-array>
strings-es
<string-array name="languagesList">
<item>inglés</item>
<item>Arábica</item>
<item>español</item>
<item>Francés</item>
<item>Chino</item>
</string-array>
strings-fr
<string-array name="languagesList">
<item>Anglais</item>
<item>arabe</item>
<item>Espagnol</item>
<item>Français</item>
<item>Chinois</item>
</string-array>
strings-zh
<string-array name="languagesList">
<item>英语</item>
<item>阿拉伯</item>
<item>西班牙语</item>
<item>法语</item>
<item>中国人</item>
</string-array>
Inside SettingsFragment user can change the app language, The buttons look like Chips, When the user clicks on Chip 1 I'll pass en, Chip 2 I'll pass ar, Chip 3 I'll pass es, etc...
I'll give the source code to the company and I don't know what languages they will add or remove, How can I make code responsive with any language?
Any ideas?

How to add all predefined languages into a ListPreference dynamically?

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>

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

Android: use res/values with String[]

I know how to use strings.xml for global strings in Android, but what's about the String[] ?
Is there any possibility to use this and if there is, how?
Thank you.
If you meant String arrays inside and xml refer here
Create a file in your values folder called arrays.xml and create your array like this
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
Not Directly in strings.xml, but something like this can be done in string.xml:
<string name="string1">str1</string>
<string name="string2">str2</string>
<string-array name="system">
<item>#string/string1</item>
<item>#string/string2</item>
</string-array>

Referencing an XML string in an XML Array (Android)

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>.

Categories

Resources