1

I have implemented a custom ListPreference and managed to load a list of items along with checkboxes for each without an issue. However, I need to add a “Select All” checkbox on top in order to select all list of items. How would I achieve this with the following source I have implemented?

The layout:

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="@string/Title_LOCATIONS">       

        <com.gm.settings.LocationsListPreference         
            android:defaultValue="null"
            android:key="list_locations"
            android:title="@string/LocationsListPreference_title" 
            android:dialogTitle="@string/LocationsListPreference_title"
            android:summary="@string/LocationsListPreference_summary"
        />

The class:

public class LocationsListPreference extends ListPreference {
}

I have implemented the class by following a tutorial and it works fine. But it uses a default layout i think and if I were to add this addition checkbox, how would I achieve this?

Update:

I want to know as to how i can add the "Select All" checkbox to the layout? Or should i create a custom layout? Please provide a sample code. (Because i feel the way it is right now, i dont have the control over this checkbox)

Pavan Welihinda
  • 605
  • 3
  • 11
  • 26
  • I guess i would check in my listener the position of the item checked in the list. If it's 0, check/uncheck all the checkbox. – Damien R. Oct 23 '13 at 12:05
  • You know how long your list is, you probably save the state of each in your SharedPreferences. So when you check the select all, kick off a method that will run through the list, getting each item by the unique key you gave to the preference item and then setting the new state into the SharedPreferences and to the item. – the-ginger-geek Oct 23 '13 at 12:17
  • Actually I want to know how to add the "Select All" Checkbox from a payout point of view? – Pavan Welihinda Oct 23 '13 at 16:48

2 Answers2

1

What you could do is add a CheckBoxPreference in your PreferenceCategory and attach to it a OnPreferenceChangedListener that sets all of the values to being checked.

An example could probably look a little something like this:

    <CheckBoxPreference
        android:key="select_all"
        android:defaultValue="false"
        android:title="Select All"
    />


    <com.gm.settings.LocationsListPreference         
        android:defaultValue="null"
        android:key="list_locations"
        android:title="@string/LocationsListPreference_title" 
        android:dialogTitle="@string/LocationsListPreference_title"
        android:summary="@string/LocationsListPreference_summary"
    />

And then in your PreferenceFragment (or PreferenceActivity), you would have the following:

SharedPreferences shareprefs = getPreferenceManager().getSharedPreferences();
LocationsListPreference listPreference = getPreference("list_locations");
CheckBoxPreference selectAll = getPreference("select_all");

selectAll.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
    public boolean onPreferenceChanged(Preference preference, Object newValue) 
    {
        //Do something with your listPreference and/or your sharedPrefs
    }
}

Hope this helps, and if you get to a road block, I think this post does a slightly better job at explaining some of the concepts. Good luck!

Community
  • 1
  • 1
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
  • thanks a lot. But what i want is the checkbox to be included inside the dialog box or inside the popup rather, where items will be loaded. E.g. When i click on "LocationsListPreference" list preference, the select all checkbox should be inside along with the list of items. – Pavan Welihinda Oct 26 '13 at 04:32
0

Found a stackoverflow post which might help others if they come across this kind of implementation:

You can build your custom ListPreference layout.

Cheers!

Community
  • 1
  • 1
Pavan Welihinda
  • 605
  • 3
  • 11
  • 26