How make a ListPreference with checkbox - android

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" />

Related

Android Preference. How to force update summary for the preference with SummaryProvider setted?

I have a modified EditTextPreference with using SimpleSummaryProvider:
<NumberPreference
android:key="pref_key"
android:defaultValue="0"
android:title="Some title"
app:useSimpleSummaryProvider="true"/>
If I change this preference programmatically (for example in the onSharedPreferenceChanged event), then the summary in the graphical interface will not update. Is there any way to notify the Preference widget that it needs to read the value again and display it in the summary? Without manually assigning a specific value to the summary?
I am using PreferenceFragmentCompat and a modified EditTextPreference with a modified EditTextPreferenceDialogFragmentCompat.
I looked at the Android sources (EditTextPreferenceDialogFragmentCompat.java) - how the dialog for changing settings notifies EditTextPreference about a value change. Did as well and it worked :)
if (editTextPreference().callChangeListener(value)) {
editTextPreference().setText(value);
}

Custom “Select All” checkbox in ListPreference

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!

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 preference change diplayed preferences in same PreferenceActivity

I have two preference xml files. The first one (pref2.xml) contains 2 preferences:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
<CheckBoxPreference
android:key="key2"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
and the other one (pref1.xml) contains 1 preference:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="key"
android:title="title"
android:defaultValue="false"
/>
</PreferenceScreen>
in my preference activity I am trying to change them:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref1);
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref2);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences"+adapter.getCount()+"(should be 2)");
actually here i get the right output and everything is displayed correctly. But I want to change the displayed preferences concerning one preference. Therefore I implemented the OnSharedPreferenceChangeListener in the preference activity:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
super.onSharedPreferenceChanged(sharedPreferences, key);
//switch the widget type
if (key.equals("key")){
this.getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.pref1);
final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
Log.d("LOG", "size of sharedPreferences "+adapter.getCount()+" (should be 1)");
}
}
Well, the pref1 preferences are displayed correctly, but the output of
"size of sharedPreferences 2"
indicated that in the background there are still the old preferences applied. If i iterate over the listAdapter i get also the old preferences.
Any idea how I could solve this?
I found out that the listadapter is some how outdated. If I get the count of items via getPreferenceScreen().getPreferenceCount() I get the right amount. But how do I access these preferences ?
I'm fairly sure that problem lies with your having two preferences with a key of "key" even though they are on different screens. A preference is identified by its key and this needs to be unique to avoid conflicts. The listener only knows which preference is being changed from the key. I would change the keys to have unique values.
EDIT: Well, wasn't right :S
...
Okey, I finally found the solution. After changing the PreferenceScreen I need to bind manually the listView again:
getPreferenceScreen().bind((ListView)findViewById(android.R.id.list));

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.

Categories

Resources