Multi select ListPreference on android - android

Any idea on implementing a multi-select (check boxes) ListPreference on Android?
Would I have to extend the ListPreference?
Is there any classes already documented to do this?
Thanks

Multi select ListPreference now comes natively with Android from API level 11 (Honeycomb).
http://developer.android.com/reference/android/preference/MultiSelectListPreference.html
Because it will be quite a while before devices have Honeycomb or later installed I'd recommend people to stick with the http://blog.350nice.com/wp/archives/240 solution.
EDIT: I think at this moment in time (almost 3 years after this answer was originally posted) you are better off using the native version now as the majority of devices have Android 4 and up.

Well , http://blog.350nice.com/wp/archives/240 does provide a solution , but a simpler solution would be just implementing a child preference screen inside the parent , and then the child preference screen can have multiple check boxes . I know , its not the best solution , but gets the job done .
For eg - the Below preference.xml
<PreferenceCategory
android:title="Regular messages"
android:key="regular_messages">
<CheckBoxPreference
android:key="enable_regular_messages"
android:summary="Enable or disable regular messages"
android:title="Send regular messages"
android:defaultValue="true"
/>
<ListPreference
android:key="send_interval"
android:title="Send interval"
android:summary="Define how often you want to send messages"
android:defaultValue="60000"
android:entries="#array/send_interval"
android:entryValues="#array/send_interval_values"
android:dependency="enable_regular_messages"
/>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Messages type"
android:key="messages_type"
android:summary="Select the type of messages to be sent"
android:dependency="enable_regular_messages">
<CheckBoxPreference
android:key="enable_status_messages"
android:summary="Enable or disable status messages"
android:title="Send status messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_event_messages"
android:summary="Enable or disable event messages"
android:title="Send event messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_critical_messages"
android:summary="Enable or disable critical messages"
android:title="Send critical messages"
android:defaultValue="true"
/>
</PreferenceScreen>
</PreferenceCategory>

Found a very useful link:
http://blog.350nice.com/wp/archives/240

Here's a single-class implementation with defaultValue support:
https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/MultiSelectListPreference.java

There is a github project just for this

Related

Not able to find "extra" attribute in jetpack preference in Android

Am trying to build Settings kind of App, I choose to use Preferences to build UI .
I am using jet pack preferences (androidx.preference.Preference) ,
If I want to launch fragment with some extras ,can use below with normal preference .
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
android:visibility="invisible"
android:fragment="com.android.xyz.MyFragment"
<extra android:name="one" android:value="first fragment" />
</com.android.xyz.ItemsTextPreference>
But in Jetpack preference , there is no "extra" attribute .
Any alternative way to send extra from preference view ?
try to wrap your extra attribute inside the intent attribute
something like this:
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
android:visibility="invisible">
<intent android:action="your_action" >
<extra android:name="one" android:value="first fragment" />
</intent>
</com.android.xyz.ItemsTextPreference>
I found solution , earlier my Preference screen having only below schema, which is from jetpack.
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
After adding one more schema from traditional preference from android, like below .
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
I can use combo to achieve result .
<com.android.xyz.ItemsTextPreference
android:key="xyz"
android:title="Fragment One"
app:isPreferenceVisible="false">
<extra android:name="one" android:value="first fragment" />
</com.android.xyz.ItemsTextPreference>

How to create Master Switch in preferences setting activity

I want to create master switch as shown in image to enable or disable all options using android preference activity.
https://i.stack.imgur.com/fsA8G.png
Something like this :- https://source.android.com/devices/tech/settings/settings-guidelines#master_setting
I didn't find a solution either but I have something similar. Maybe it helps.
I have this on top:
<PreferenceCategory app:title="">
<SwitchPreferenceCompat
app:key="MasterSwitch"
app:summaryOff="Everything is disabled"
app:summaryOn="Everything is enabled"
app:title="This is a master switch (more or less)"
/>
</PreferenceCategory>
And then, every PreferenceCategory depends on the above (app:dependency="MasterSwitch"). So, if the "MasterSwitch" is disabled, all categories below are displayed as disabled. Set the Categories this way:
<PreferenceCategory
app:dependency="MasterSwitch"
app:title="Another category">
<EditTextPreference
app:key="signature"
app:title="Option 1"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
and so on...
The problem is that I cannot set the nice blue or gray background color that you showed here: https://source.android.com/devices/tech/settings/settings-guidelines#master_setting
I cannot have the information box it is shown there either.
You can do this in Settings Activity or Fragment. There are a lot of tutorials on YouTube.
New -> Activity -> Settings Activity

How create a settings menu with checkbox Android

I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="#string/summary_category"
android:title="#string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="#string/summary_main"
android:title="#string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="#string/summary_firstDependent"
android:title="#string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="#string/summary_secondDependent"
android:title="#string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="#string/summary_first"
android:title="#string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="#string/summary_second"
android:title="#string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="#string/summary_third"
android:title="#string/title_third" />
</PreferenceCategory>

List from SQLite in a preferences.xml

i'm a new android developer and i hope that you guys could help me.
I'm developing a preference screen like above:
<PreferenceCategory android:title="#string/security_title" >
<EditTextPreference
android:name="Password"
android:defaultValue=""
android:key="passwordPref"
android:password="true"
android:summary="#string/security_sumary"
android:title="#string/security_addPassword" />
<PreferenceScreen
android:key="secondPrefScreenPref"
android:summary="Click here to go to the second Preference Screen"
android:title="Second Preference Screen" >
<ListPreference android:entries="#android:id/list" android:key="listPasswordKey" />
</PreferenceScreen>
</PreferenceCategory>
In a password field,the user are going to type his passw and when he clicks OK button, the program are gonna save it in my database. This part is ok!
But when he clicks on a second pref screen i want to list, from database, all your passwords saved.
I know how to load the data, but i don't know how to call my method when the user clicks on a SecondPrefScreen and how to show them in a preferences.xml
can anyone help me?
thx
You will have to create a widget that will load the data and list it for you. Essentially, create an object that extends DialogPreference and give it a child list view that will list your security credentials.

Set a check box to false in Preference Activity

I am trying to implement user preferences. A problem I have is that if one of my check boxes is ticked I want to set the other to false. I know about dependency and from what I have seen it doesn't change the value. Just disables you from changing the preference. What can I do to change a checkbox to false if another is true? This is my xml:
<PreferenceCategory android:title="Launch Settings">
<CheckBoxPreference
android:key="enable_nearest_station"
android:title="Nearest station"
android:defaultValue="false"
android:summary="Will launch your nearest station on startup"
android:disableDependentsState="true"/>
<CheckBoxPreference
android:key="enable_default_station"
android:title="Default station"
android:defaultValue="false"
android:dependency="enable_nearest_station"
android:summary="Allows you to choose a default to launch with."
/>
/>
</PreferenceCategory>
Thanks in advance.
Use the set setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener) to your CheckBoxPreference and code to set false other CheckBoxPreference.

Categories

Resources