Custom “Select All” checkbox in ListPreference - android

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)

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!

Found a stackoverflow post which might help others if they come across this kind of implementation:
You can build your custom ListPreference layout.
Cheers!

Related

Building Preference screen in code depending on another setting

I have searched here and looked at samples but haven't yet found an answer to what is essentially a simple problem. Depending on the choice made in a preceding ListPreference, I want to build a preference screen of CheckBoxPreferences dynamically in code, which is then shown when I click on a simple preference with a click listener. The list and number of check boxes will be different in each case.
This is where I have got to so far - just a simple bit of code to test the concept in the onClick listener, but how to get the check box preference screen to appear? There must be a simple explanation why it doesn't. What am I doing wrong?
Part of my xml code:
<PreferenceCategory android:title="Filters">
<PreferenceScreen android:key="FilterScreen"
android:title="Filters" android:summary="Click to change filter settings">
<ListPreference android:title="Filter type"
android:summary="Set to: Gliding"
android:key="filterType"
android:defaultValue="0"
android:entries="#array/filterTypeOptions"
android:entryValues="#array/filterTypeValues" />
<CheckBoxPreference android:title=""
android:summary="Include Aerodrome Notams"
android:defaultValue="false" android:key="filterIncludeAerodrome" />
<CheckBoxPreference android:title=""
android:summary="Delete night-time Notams"
android:defaultValue="true" android:key="filterDeleteNighttime" />
<ListPreference android:title="Select category to change"
android:summary="Set to: Airspace organisation"
android:key="filterCategory"
android:defaultValue="0"
android:entries="#array/filterCategoryOptions"
android:entryValues="#array/filterCategoryValues" />
<Preference android:title="Show filters for category"
android:summary="Click to choose subjects to delete"
android:key="filterShow" />
</PreferenceScreen>
</PreferenceCategory>
The contents of "Show filters for category" will depend on the "Filter type" and "Select category to change" settings.
This is the simple test code I have for the "Show filters" click listener (cut down just to show essentials):
public class Settings extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
------
public static final String KEY_FILTER_SHOW = "filterShow";
------
private Preference mFilterShow;
------
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.settings);
// Get a reference to the preferences
------
mFilterShow = (Preference)findPreference(KEY_FILTER_SHOW);
------
// Set the click listener for Show Filter options
mFilterShow.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference pref)
{
Context ctx = pref.getContext();
PreferenceScreen screen =
pref.getPreferenceManager().createPreferenceScreen(ctx);
CheckBoxPreference cb1 = new CheckBoxPreference(ctx);
cb1.setTitle("This is cb1");
cb1.setKey("cb1_key");
cb1.setDefaultValue(false);
screen.addPreference(cb1);
return true;
}
});
OK, I have solved the problem myself, through a process of iteration! Others might find this useful.
Just create an empty PreferenceScreen in the xml:
<PreferenceScreen android:title="Show filters for category"
android:summary="Click to choose subjects to delete"
android:key="filterShow">
</PreferenceScreen>
Then in the code there is no need for the onClick listener - the contents of the screen are created in the onCreate function. Actually, since the contents of the screen need to change when the choice made in the Category list preference (see original code) changes, this needs to go in a separate function which is called both from onCreate and onSharedPreferenceChanged:
public static final String KEY_FILTER_SHOW = "filterShow";
...
private PreferenceScreen mFilterShow;
...
// In onCreate:
// Get a reference to the PreferenceScreen
mFilterShow =
(PreferenceScreen)getPreferenceScreen().findPreference(KEY_FILTER_SHOW);
// Now the code to create the contents of the screen
mFilterShow.removeAll();
CheckBoxPreference cb1 = new CheckBoxPreference(this);
cb1.setTitle("This is cb1");
cb1.setKey("cb1_key");
cb1.setDefaultValue(true);
mFilterShow.addPreference(cb1);
The above is just "proof of concept". It works exactly as you would expect. In my final version, I will create an array of CheckBoxPreferences with 'new' initially, then re-use them (changing title and default) when setting up the contents of the screen for each Category choice as it changes. The number of check boxes required may be different for each category - I will create an array for the maximum number required, then add as many as I need in each case.

Android - How to change texts in the preferences activity dynamically?

Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="#string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="#string/SOME_SUMMARY"
android:title="#string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example:
We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
You can create a subclass of ListPreference in which you set an OnPreferenceChangedListener from which you will have access to the new value, and set the text on your ListPreference. I think the setSummary() function on the ListPreference will update the text under the name of the preference. If that doesn't work you can also override getView() to implement your own custom view for the Preference on which you can set the text directly.

Android CheckBoxPreference: how to disable and enable other preferences on preference change

I have CheckBoxPreference and 2 others: one is Edit Test Pref. and another is ListBox Pref. How I can enable list box pref and disable edit text pref. when CheckBoxPreference is turned on?
As a variant, it's possible to put the "dependency" into the ListBoxPref.
<PreferenceCategory
android:key="key1"
android:title="#string/title1">
<SwitchPreference
android:key="parents"
android:summaryOff="#string/do_smthng"
android:summaryOn="#string/do_smthng"
android:switchTextOff="#string/i_am_sleeping"
android:switchTextOn="#string/i_have_free_time" />
<CheckBoxPreference
android:key="baby"
android:dependency="parents"
android:title="#string/basic_habbits"
android:summaryOff="#string/play_with_parents"
android:summaryOn="#string/play_with_parents"
android:defaultValue="true" />
</PreferenceCategory>
Basically, baby can't play with parents when they are sleeping =)
Seems it's duplicate of this question
You can override public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) and when it get called with key.equals(<Your key for setting>) do something like:
boolean isEnabled = sharedPreferences.getBoolean(key, true);
getPreferenceScreen().findPreference("list box preference key").setEnabled(isEnabled);
getPreferenceScreen().findPreference("list box preference key").setEnabled(!isEnabled);
Also, do the same in onCreate() to have Your preferences screen proper initial setup.
You can get the checkbox value. And then, to enable/disable the preferences, you can use pref.setEnabled(false); To enable, just use the same function and put the true value.
Just to simplify the answer from Sergio,
android:dependency="keyOfParent"
This makes the item dependent on the parent item, may need to be a switch.
Also adding a listener to the onSharedPreferenceChanged works, sometimes.. it sometimes doesn't work as desired ( not sure why )
Add
public class YourClass extends Whatever implements SharedPreferences.OnSharedPreferenceChangeListener
Then after OnCreate()
#Override
public void onSharedPreferenceChanged (SharedPreferences p1, String p2)
{
if (Your Arguments)
{
// getPreferenceScreen().findPreference("pref_key").setEnabled(false);
}
}

Nested preferences.xml

Is it somehow possible to include one preferences.xml into another, like it can be done for layouts with the <include /> tag?
Let's say:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:title="#string/pref_group_title_visual">
<include
preferences_filename="xml/pref_visual"/>
</PreferenceScreen>
...
Solution here it is to inflate both preference files from PreferencesActivity. For example:
addPreferencesFromResource(R.xml.options);
addPreferencesFromResource(R.xml.additional_options);
The solution soul shows works. It can be expanded to only show preferences if you're the developer using an unsigned version of the app ;)
addPreferencesFromResource(R.xml.options);
addPreferencesFromResource(R.xml.additional_options);
if (BuildConfig.DEBUG) {
addPreferencesFromResource(R.xml.developer_options);
}
I created a blog post regarding this issue and have a complete working code example available for download.
http://androidfu.blogspot.com/2012/05/developer-debug-with-nested-preferences.html
To truly achieve the nesting effect you can use this technique to relocate the loaded preferences to a group already loaded.
PreferenceCategory notifications = (PreferenceCategory) getPreferenceScreen ().findPreference (PreferenceKey.pref_notifications.name ());
addPreferencesFromResource (R.xml.pref_notifications, notifications);
Where the enhanced addPreferencesFromResource is defined as:
private void addPreferencesFromResource (int id, PreferenceGroup newParent) {
PreferenceScreen screen = getPreferenceScreen ();
int last = screen.getPreferenceCount ();
addPreferencesFromResource (id);
while (screen.getPreferenceCount () > last) {
Preference p = screen.getPreference (last);
screen.removePreference (p); // decreases the preference count
newParent.addPreference (p);
}
}
It works for any PreferenceGroup such as PreferenceScreen and PreferenceCategory.
No, it seems to be impossible. But there's a simple workaround. You can make another PreferenceActivity that loads nested PreferenceScreen. And in the main preference.xml file you need to create a Preference object and set an Intent object for it in code (using setIntent() method). This Intent must be used to create the second PreferenceActivity.

How make a ListPreference with checkbox

How can I create a ListPreference with checkbox?
I know how to use ListPreference, but I need multiple selection like in Alarm application on "repeat" preference.
like this screenshot:
Since API 11 you can use MultiSelectListPreference
String[] selections = {"selection1","Selection2"};
Set<String> selectionSet = new HashSet<String>();
selectionSet.addAll(Arrays.asList(selections));
MultiSelectListPreference multiSelectPref = new MultiSelectListPreference(this);
multiSelectPref.setKey("multi_pref");
multiSelectPref.setTitle("Multi Select List Preference");
multiSelectPref.setEntries(selections);
multiSelectPref.setEntryValues(selections);
multiSelectPref.setDefaultValue(selectionSet);
getPreferenceScreen().addPreference(multiSelectPref);
Use, MultiSelectListPreference
<MultiSelectListPreference
app:defaultValue="#array/watermark_entries_view"
app:dialogTitle="Select Watermark Type"
app:entries="#array/watermark_entries_view"
app:entryValues="#array/watermark_entries_values"
app:key="mode_repeat"
app:summary="Enable Watermark"
app:title="Watermark" />
There is no built-in preference for that AFAIK. ListPreference is single-select only.
You could create your own custom Preference class, though, by extending DialogPreference.
For boolean values you must use a SwitchPreference, as follows:
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="#string/pref_description_social_recommendations"
android:title="#string/pref_title_social_recommendations" />

Categories

Resources