1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.ui;
7 import android.content.Context;
8 import android.graphics.Color;
9 import android.util.AttributeSet;
10 import android.widget.ListView;
12 import org.chromium.ui.ColorSuggestionListAdapter.OnColorSuggestionClickListener;
15 * Draws a grid of (predefined) colors and allows the user to choose one of
18 public class ColorPickerSimple extends ListView implements OnColorSuggestionClickListener {
20 private OnColorChangedListener mOnColorChangedListener;
22 private static final int[] DEFAULT_COLORS = {
33 private static final int[] DEFAULT_COLOR_LABEL_IDS = {
34 R.string.color_picker_button_red,
35 R.string.color_picker_button_cyan,
36 R.string.color_picker_button_blue,
37 R.string.color_picker_button_green,
38 R.string.color_picker_button_magenta,
39 R.string.color_picker_button_yellow,
40 R.string.color_picker_button_black,
41 R.string.color_picker_button_white
44 public ColorPickerSimple(Context context) {
48 public ColorPickerSimple(Context context, AttributeSet attrs) {
49 super(context, attrs);
52 public ColorPickerSimple(Context context, AttributeSet attrs, int defStyle) {
53 super(context, attrs, defStyle);
57 * Initializes the listener and sets the adapter for the given list of suggestions. If the
58 * suggestions is null a default set of colors will be used.
60 * @param suggestions The list of suggestions that should be displayed.
61 * @param onColorChangedListener The listener that gets notified when the user touches
64 public void init(ColorSuggestion[] suggestions,
65 OnColorChangedListener onColorChangedListener) {
66 mOnColorChangedListener = onColorChangedListener;
68 if (suggestions == null) {
69 suggestions = new ColorSuggestion[DEFAULT_COLORS.length];
70 for (int i = 0; i < suggestions.length; ++i) {
71 suggestions[i] = new ColorSuggestion(DEFAULT_COLORS[i],
72 getContext().getString(DEFAULT_COLOR_LABEL_IDS[i]));
76 ColorSuggestionListAdapter adapter = new ColorSuggestionListAdapter(
77 getContext(), suggestions);
78 adapter.setOnColorSuggestionClickListener(this);
83 public void onColorSuggestionClick(ColorSuggestion suggestion) {
84 mOnColorChangedListener.onColorChanged(suggestion.mColor);