this.removeAllViews(); int tableElements = 0; int rowElements = 0; TableRow row = createTableRow(); for (int color : colors) { tableElements++; View colorSwatch = createColorSwatch(color, selectedColor); setSwatchDescription(rowNumber, tableElements, rowElements, color == selectedColor, colorSwatch); addSwatchToRow(row, colorSwatch, rowNumber); addView(row); row = createTableRow(); rowElements = 0; rowNumber++; addSwatchToRow(row, createBlankSpace(), rowNumber); rowElements++; addView(row);
private void refreshPalette() { if (mPalette != null && mColors != null) { mPalette.drawPalette(mColors, mSelectedColor); } }
/** * Creates a blank space to fill the row. */ private ImageView createBlankSpace() { ImageView view = new ImageView(getContext()); TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength); params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize); view.setLayoutParams(params); return view; }
public void onGlobalLayout() { if (this.ignoreNextUpdate) { this.ignoreNextUpdate = false; return; } // Calculate number of swatches to display int swatchSize = ColorPreference.this.palette.getResources().getDimensionPixelSize(R.dimen.color_swatch_small); ColorPreference.this.palette.init(ColorPickerDialog.SIZE_SMALL, (view.getWidth() - (swatchSize * 2 / 3)) / swatchSize, ColorPreference.this); // Cause redraw and (by extension) also a layout recalculation this.ignoreNextUpdate = true; ColorPreference.this.drawPalette(); } });
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null); mProgress = (ProgressBar) view.findViewById(android.R.id.progress); mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker); mPalette.init(mSize, mColumns, this); if (mColors != null) { showPaletteView(); } mAlertDialog = new AlertDialog.Builder(activity) .setTitle(mTitleResId) .setView(view) .create(); return mAlertDialog; }
/** * Initialize the size, columns, and listener. Size should be a pre-defined size (SIZE_LARGE * or SIZE_SMALL) from ColorPickerDialogFragment. */ public void init(int size, int columns, OnColorSelectedListener listener) { mNumColumns = columns; Resources res = getResources(); if (size == ColorPickerDialog.SIZE_LARGE) { mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_large); mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_large); } else { mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_small); mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_small); } mOnColorSelectedListener = listener; mDescription = res.getString(R.string.color_swatch_description); mDescriptionSelected = res.getString(R.string.color_swatch_description_selected); }
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); View view = View.inflate(activity, R.layout.color_picker_dialog, null); mProgress = view.findViewById(android.R.id.progress); mPalette = view.findViewById(R.id.color_picker); mPalette.init(mSize, mColumns, this); if (mColors != null) { showPaletteView(); } return new AlertDialog.Builder(activity) .setTitle(mTitleResId) .setView(view) .create(); }
/** * Initialize the size, columns, and listener. Size should be a pre-defined size (SIZE_LARGE * or SIZE_SMALL) from ColorPickerDialogFragment. */ public void init(int size, int columns, OnColorSelectedListener listener) { mNumColumns = columns; Resources res = getResources(); if (size == ColorPickerDialog.SIZE_LARGE) { mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_large); mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_large); } else { mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_small); mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_small); } mOnColorSelectedListener = listener; mDescription = res.getString(R.string.color_swatch_description); mDescriptionSelected = res.getString(R.string.color_swatch_description_selected); }
this.removeAllViews(); int tableElements = 0; int rowElements = 0; TableRow row = createTableRow(); for (int color : colors) { View colorSwatch = createColorSwatch(color, selectedColor); setSwatchDescription(rowNumber, tableElements, rowElements, color == selectedColor, colorSwatch, colorContentDescriptions); addSwatchToRow(row, colorSwatch, rowNumber); addView(row); row = createTableRow(); rowElements = 0; rowNumber++; addSwatchToRow(row, createBlankSpace(), rowNumber); rowElements++; addView(row);
private TableRow createTableRow() { TableRow row = new TableRow(getContext()); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); row.setLayoutParams(params); return row; }
@Override public void onColorSelected(int color) { if (mListener != null) { mListener.onColorSelected(color); } if (getTargetFragment() instanceof OnColorSelectedListener) { final OnColorSelectedListener listener = (OnColorSelectedListener) getTargetFragment(); listener.onColorSelected(color); } if (color != mSelectedColor) { mSelectedColor = color; // Redraw palette to show checkmark on newly selected color before dismissing. mPalette.drawPalette(mColors, mSelectedColor); } dismiss(); }
this.palette.init(ColorPickerDialog.SIZE_SMALL, 4, this);
/** * Creates a color swatch. */ private ColorPickerSwatch createColorSwatch(int color, int selectedColor) { ColorPickerSwatch view = new ColorPickerSwatch(getContext(), color, color == selectedColor, mOnColorSelectedListener); TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength); params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize); view.setLayoutParams(params); return view; } }
/** * Adds swatches to table in a serpentine format. */ public void drawPalette(int[] colors, int selectedColor) { drawPalette(colors, selectedColor, null); }
/** * Creates a blank space to fill the row. */ private ImageView createBlankSpace() { ImageView view = new ImageView(getContext()); TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength); params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize); view.setLayoutParams(params); return view; }
private void drawPalette() { if (this.palette != null) { this.palette.drawPalette(UIColors.COLOR_LIST, this.selectedColor); } }
private TableRow createTableRow() { TableRow row = new TableRow(getContext()); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); row.setLayoutParams(params); return row; }
private void refreshPalette() { if (mPalette != null && mColors != null) { mPalette.drawPalette(mColors, mSelectedColor, mColorContentDescriptions); } }
/** * Creates a color swatch. */ private ColorPickerSwatch createColorSwatch(int color, int selectedColor) { ColorPickerSwatch view = new ColorPickerSwatch(getContext(), color, color == selectedColor, mOnColorSelectedListener); TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength); params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize); view.setLayoutParams(params); return view; } }
@Override public void onColorSelected(int color) { if (mListener != null) { mListener.onColorSelected(color); } if (getTargetFragment() instanceof OnColorSelectedListener) { final OnColorSelectedListener listener = (OnColorSelectedListener) getTargetFragment(); listener.onColorSelected(color); } if (color != mSelectedColor) { mSelectedColor = color; // Redraw palette to show checkmark on newly selected color before dismissing. mPalette.drawPalette(mColors, mSelectedColor); } dismiss(); }