Is it possible to create an individual preference in an PreferenceScreen
?
I would like to code color settings like that:
I know that choosing the color is easy realizable with the ListPreference
, but it would be awesome with that kind of "checkboxes".
Creating a custom preference is similar to creating a fragment or other UI components, by defining views and actions.
Android developers has a good guide on creating settings, which includes a section for creating custom preferences: http://developer.android.com/guide/topics/ui/settings.html#Custom
Or a better alternative will be to extend the DialogPreference class. You can set a color picker widget as the dialog view and get the positive result inside the extended preference itself. Here is a similar question, but it is very useful for your scenario.
The Android Developer page only shows how to make a
DialogFragment
. It's still possible to customise the appearance of aPreference
item though. In your XML you have to declare the root element asandroid:id="@android:id/widget_frame
, and then declareTextView
asandroid:title
andandroid:summary
. You can then declare other elements you want to appear in the layout. Here's an example showing aSeekBar
which you could easily adapt to a multi-checkbox colour chooser.seekbar_preference.xml
Then, in a class that derives from
Preference
, override theonCreateView()
method:SeekbarPreference.java
Then in the preferences.xml file use the preference:
preferences.xml
This gives a preference that looks as follows:
You can easily adapt this method to show multiple checkboxes on a row as was in the original question.
This is how I do it, using support library
preference-v7
.Preference
and overrideonBindViewHolder()
. This method allows you to get references to your preference's view via theViewHolder
object.setWidgetLayoutResourece()
orsetLayoutResource()
in constructor.layout/preference_theme.xml
PreferenceTheme.java (custom Preference class)
preferences.xml
You can create your custom layout for preference and you can set it in
android:layout
attribute in Preference in res/xml like this:Or you can use an Activity instead of preference