10 ColorPalettePreference - michael-rapp/AndroidMaterialPreferences GitHub Wiki

The preference ColorPalettePreference allows to choose a color from a predefined color palette. Therefore the preference's dialog shows previews of the available colors, which can be selected by the user, in a scrollable grid. Additionally, when the dialog is not shown, a preview of the currently selected color can be shown at the right edge of the preference. The previews of colors in the preference itself, as well as in its dialog, can be flexibly customized: The colors can be shown as a round or square shape and the width and color of the border surrounding it can be changed. By default the previews have a tiled background, which becomes visibe, if the previewed color is transparent. This background can also be changed. When the preference's showValueAsSummary attribute is set to true, a textual representation of the currently selected color is shown as its summary. The format of the textual representation can be chosen freely: The color's hexadecimal value or its RGB components can be printed.

10.1 Getter and setter methods

The class ColorPalettePreference provides the following getter and setter methods, which can be used to modify the preference's properties.

Getter and setter methods Description Default value Restrictions
isPreviewShown():boolean showPreview(boolean):void Returns or sets, whether a preview of the currently persisted color should be shown at the right edge of the preference. true none
getPreviewSize():int setPreviewSize(int):void Returns or sets the size of the preview, which is shown at the right edge of the preference, in pixels. 38dp The size must be at least 1.
getPreviewShape():PreviewShape setPreviewShape(PreviewShape):void Returns or sets the shape of the preview, which is shown at the right edge of the preference. It may be CIRCLE or SQUARE. PreviewShape.CIRCLE The shape may not be null.
getPreviewBorderWidth():int setPreviewBorderWidth(int):void Returns or sets the width of the border of the preview, which is shown at the right edge of the preference. 1dp The border width must be at least 0.
getPreviewBorderColor():int setPreviewBorderColor(int):void Returns or sets the color of the border of the preview, which is shown at the right edge of the preference. #ffd2d2d2 none
getPreviewBackground():Drawable setPreviewBackground(Drawable):void setPreviewBackground(int):void setPreviewBackgroundColor(int):void Returns or sets the background of the preview, which is shown at the right edge of the preference. R.drawable.color_picker_default_background If the background is specified as a resource ID, the ID must correspond to a valid drawable resource.
getColorFormat():ColorFormat setColorFormat(ColorFormat):void Returns or sets the format, which is used to print a textual representation of the preference's color. It may be RGB, ARGB, HEX_3_BYTES or HEX_4_BYTES. ColorFormat.HEX_4_BYTES The format may not be null.
getDialogPreviewSize():int setDialogPreviewSize(int):void Returns or sets the size of the previews, which are shown in the preference's dialog. 72dp (96dp on tablets) The size must be at least 1.
getDialogPreviewShape():PreviewShape setDialogPreviewShape(int):PreviewShape Returns or sets the shape of the previews, which are shown in the preference's dialog. It may be CIRCLE or SQUARE. PreviewShape.CIRCLE The shape may not be null.
getDialogPreviewBorderWidth():int setDialogPreviewWidth(int):void Returns or sets the border width of the previews, which are shown in the preference's dialog. 1dp The border width must be at least 0.
getDialogPreviewBorderColor():int setDialogPreviewColor(int):void Returns or sets the border color of the previews, which are shown in the preference's dialog. #ffd2d2d2 none
getDialogPreviewBackground():Drawable setDialogPreviewBackground(Drawable):void setDialogPreviewBackground(int):void setDialogPreviewBackgroundColor(int):void Returns or sets the background of the previews, which are shown in the preference's dialog. R.drawable.color_picker_default_background If the background is specified as a resource ID, the ID must correspond to a valid drawable resource.
getNumberOfColumns():int setNumberOfColumns(int):void Returns or sets the number of columns of the grid, which is used to show the previews, which are shown in the preference's dialog. 3 (4 on tablets) The number of columns must be at least 1.
getColorPalette():int[] setColorPalette(int[]):void setColorPalette(int):void Returns or sets the colors, the preference allows to choose. [] If the color palette is specified as a resource ID, the ID must correspond to an integer array resource.
getColor():int setColor(int):void Returns or sets the color of the preference. If a color is set, it is instantly persisted. 0 none

10.2 XML attributes

As an alternative to using the setter methods mentioned above, the following XML attributes can be used to set the properties of a ColorPalettePreference.

XML attribute Description Format
custom:showPreview Whether a preview of the currently persisted color should be shown at the right edge of the preference. boolean
custom:previewSize The size of the preview, which is shown at the right edge of the preference. dimension
custom:previewShape The shape of the preview, which is shown at the right edge of the preference. enum
custom:previewBorderWidth The border width of the preview, which is shown at the right edge of the preference. dimension
custom:previewBorderColor The border color of the preview, which is shown at the right edge of the preference. color
custom:previewBackground The background of the preview, which is shown at the right edge of the preference. reference
custom:colorFormat The format, which is used to print a textual representation of the preference's color. enum
custom:dialogPreviewSize The size of the previews, which are shown in the preference's dialog. dimension
custom:dialogPreviewShape The shape of the previews, which are shown in the preference's dialog. enum
custom:dialogPreviewBorderWidth The border width of the previews, which are shown in the preference's dialog. dimension
custom:dialogPreviewBorderColor The border color of the previews, which are shown in the preference's dialog. color
custom:dialogPreviewBackground The background of the previews, which are shown in the preference's dialog. reference
custom:numberOfColumns The number of columns of the grid, which is used to show the previews, which are shown in the preference's dialog. integer
custom:colorPalette The colors, the preference allows to choose. reference
android:defaultValue The default color of the preference. color