forked from kristiyanP/colorpicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPicker.java
More file actions
463 lines (408 loc) · 14.5 KB
/
ColorPicker.java
File metadata and controls
463 lines (408 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package petrov.kristiyan.colorpicker;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Surface;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import me.drakeet.materialdialog.MaterialDialog;
public class ColorPicker {
private OnChooseColorListener onChooseColorListener;
private OnFastChooseColorListener onFastChooseColorListener;
private OnButtonListener onNegativeButtonListener,onPositiveButtonListener;
public interface OnChooseColorListener {
void onChooseColor(int position,int color);
}
public interface OnFastChooseColorListener {
void setOnFastChooseColorListener(int position,int color);
}
public interface OnButtonListener{
void onClick(View v);
}
private ArrayList<ColorPal> colors;
private ColorViewAdapter colorViewAdapter;
private boolean fastChooser;
private TypedArray ta;
private Activity activity;
private int columns;
private String title;
private int gravity = Gravity.CENTER;
private int marginLeft, marginRight, marginTop, marginBottom;
private int tickColor;
private int marginButtonLeft, marginButtonRight, marginButtonTop, marginButtonBottom;
private int buttonWidth, buttonHeight;
private int buttonDrawable;
private String negativeText = "CANCEL", positiveText = "OK";
private boolean roundButton;
private boolean dismiss;
private boolean fullheight;
private MaterialDialog mMaterialDialog;
private RecyclerView recyclerView;
private int default_color = 0;
private int paddingTitleLeft,paddingTitleRight,paddingTitleBottom,paddingTitleTop;
/**
* Constructor
* @param activity Activity calling
*/
public ColorPicker(Activity activity) {
this.activity = activity;
this.dismiss = true;
this.marginButtonLeft = this.marginButtonTop = this.marginButtonRight = this.marginButtonBottom = dip2px(5);
this.title ="Choose the color";
this.columns = 5;
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int wSizeTemp;
/** Now supports all orientations */
switch (rotation) {
case Surface.ROTATION_0:
//"SCREEN_ORIENTATION_PORTRAIT";
wSizeTemp = metrics.widthPixels;
break;
case Surface.ROTATION_90:
//"SCREEN_ORIENTATION_LANDSCAPE";
wSizeTemp = metrics.heightPixels;
break;
case Surface.ROTATION_180:
//"SCREEN_ORIENTATION_REVERSE_PORTRAIT";
wSizeTemp = metrics.widthPixels;
break;
case Surface.ROTATION_270:
//"SCREEN_ORIENTATION_REVERSE_LANDSCAPE";
wSizeTemp = metrics.heightPixels;
break;
default:
//"SCREEN_ORIENTATION_PORTRAIT";
wSizeTemp = metrics.widthPixels;
break;
}
/*DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);*/
//int windowSize = metrics.widthPixels;
int windowSize = wSizeTemp;
this.paddingTitleTop = this.paddingTitleBottom = this.paddingTitleLeft = this.paddingTitleRight = ( windowSize - ( columns * dip2px(5) + columns * dip2px(40) ) )/2 - (int)((windowSize * 0.25)/2);
}
/**
* Set buttons color using a resource array of colors example : check in library res/values/colorpicker-array.xml
* @param resId Array resource
* @return this
*/
public ColorPicker setColors(int resId) {
ta = activity.getResources().obtainTypedArray(resId);
colors = new ArrayList<>();
for (int i = 0; i < ta.length(); i++) {
colors.add(new ColorPal(ta.getColor(i, 0), false));
}
return this;
}
/**
* Set default colors defined in colorpicker-array.xml of the library
* @return this
*/
private ColorPicker setColors() {
ta = activity.getResources().obtainTypedArray(R.array.default_colors);
colors = new ArrayList<>();
for (int i = 0; i < ta.length(); i++) {
colors.add(new ColorPal(ta.getColor(i, 0), false));
}
return this;
}
/**
* Set buttons from an arraylist of Hex values
* @param colorsHexList List of hex values of the colors
* @return this
*/
public ColorPicker setColors(ArrayList<String> colorsHexList) {
colors = new ArrayList<>();
for (int i = 0; i < colorsHexList.size(); i++) {
colors.add(new ColorPal(colorsHexList.indexOf(i), false));
}
return this;
}
/**
* Set buttons color Example : Color.RED,Color.BLACK
* @param colorsList list of colors
* @return this
*/
public ColorPicker setColors(int... colorsList) {
colors = new ArrayList<>();
for (int i = 0; i < colorsList.length; i++) {
colors.add(new ColorPal(colorsList[i], false));
}
return this;
}
/**
* Show the Material Dialog
*/
public void show() {
if (colors == null || colors.isEmpty())
setColors();
View view = activity.getLayoutInflater().inflate(R.layout.color_palette_layout, null);
TextView titleView = (TextView) view.findViewById(R.id.title);
if (title != null) {
titleView.setText(title);
titleView.setPadding(paddingTitleLeft,paddingTitleTop,paddingTitleRight,paddingTitleBottom);
}
//create Material Dialog if posneg is not enabled
mMaterialDialog = new MaterialDialog(activity);
recyclerView = (RecyclerView) view.findViewById(R.id.color_palette);
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, columns);
recyclerView.setLayoutManager(gridLayoutManager);
if( fastChooser )
colorViewAdapter = new ColorViewAdapter(colors,onFastChooseColorListener);
else
colorViewAdapter = new ColorViewAdapter(colors);
if(fullheight) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.BELOW,titleView.getId());
lp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
recyclerView.setLayoutParams(lp);
}
recyclerView.setAdapter(colorViewAdapter);
if (marginBottom != 0 || marginLeft != 0 || marginRight != 0 || marginTop != 0) {
colorViewAdapter.setMargin(marginLeft, marginTop, marginRight, marginBottom);
}
if (tickColor != 0) {
colorViewAdapter.setButtonsTickColor(tickColor);
}
if (marginButtonBottom != 0 || marginButtonLeft != 0 || marginButtonRight != 0 || marginButtonTop != 0) {
colorViewAdapter.setButtonMargin(marginButtonLeft, marginButtonTop, marginButtonRight, marginButtonBottom);
}
if (buttonHeight != 0 || buttonWidth != 0) {
colorViewAdapter.setButtonSize(buttonWidth, buttonHeight);
}
if (roundButton) {
this.setButtonDrawable(R.drawable.round_button);
}
if (buttonDrawable != 0) {
colorViewAdapter.setButtonDrawable(buttonDrawable);
}
if ( default_color != 0 ){
colorViewAdapter.setDefaultColor(default_color);
}
if( !fastChooser || onNegativeButtonListener != null || onPositiveButtonListener != null ) {
mMaterialDialog
.setPositiveButton(positiveText, new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!fastChooser)
onChooseColorListener.onChooseColor(colorViewAdapter.getColorPosition(), colorViewAdapter.getColorSelected());
onPositiveButtonListener.onClick(v);
if (dismiss)
mMaterialDialog.dismiss();
}
})
.setNegativeButton(negativeText, new View.OnClickListener() {
@Override
public void onClick(View v) {
onNegativeButtonListener.onClick(v);
if (dismiss)
mMaterialDialog.dismiss();
}
}).setView(view);
}
else
mMaterialDialog.setView(view);
mMaterialDialog.show();
if(positiveText.isEmpty()){
mMaterialDialog.getNegativeButton().setPadding(dip2px(12), 0, dip2px(32),0);
}
}
/**
* Define the number of columns by default value= 3
* @param c Columns number
* @return this
*/
public ColorPicker setColumns(int c) {
columns = c;
return this;
}
/**
* Define the title of the Material Dialog
* @param title Title
* @return this
*/
public ColorPicker setTitle(String title) {
this.title = title;
return this;
}
/**
* Define the gravity of the columns example : Gravity.CENTER, Gravity.LEFT , Gravity.RIGHT ...
* @param gravity Gravity
* @return this
*/
public ColorPicker setGravity(int gravity) {
this.gravity = gravity;
return this;
}
/**
* Define the margins of the Material Dialog in PIXEL
* @param left left
* @param top top
* @param right right
* @param bottom bottom
* @return this
*/
public ColorPicker setMargin(int left, int top, int right, int bottom) {
this.marginLeft = left;
this.marginRight = right;
this.marginTop = top;
this.marginBottom = bottom;
return this;
}
/**
* Set tick color
* @param color Color
* @return this
*/
public ColorPicker setButtonsTickColor(int color) {
this.tickColor = color;
return this;
}
/**
* Set a single drawable for all buttons example : you can define a different shape ( then round or square )
* @param drawable
* @return this
*/
public ColorPicker setButtonDrawable(int drawable) {
this.buttonDrawable = drawable;
return this;
}
/**
* Set the buttons size in PIXEL
* @param width width
* @param height height
* @return this
*/
public ColorPicker setButtonSize(int width, int height) {
this.buttonWidth = width;
this.buttonHeight = height;
return this;
}
/**
* Set the Margin between the buttons in PIXEL default is 10
* @param left left
* @param top top
* @param right right
* @param bottom bottom
* @return
*/
public ColorPicker setButtonMargin(int left, int top, int right, int bottom) {
this.marginButtonLeft = left;
this.marginButtonTop = top;
this.marginButtonRight = right;
this.marginButtonBottom = bottom;
return this;
}
/**
* Set title of the positive button in the Material Dialog
* @param text text
* @return this
*/
public ColorPicker setPositiveButton(String text,OnButtonListener listener) {
this.positiveText = text;
this.onPositiveButtonListener = listener;
return this;
}
/**
* Set the negative button in the Material Dialog usable also with fastChooser
* @param text text
* @return this
*/
public ColorPicker setNegativeButton(String text , OnButtonListener listener) {
this.negativeText = text;
this.onNegativeButtonListener = listener;
return this;
}
/**
* Set round button
* @param roundButton true if you want a round button
* @return this
*/
public ColorPicker setRoundButton(boolean roundButton) {
this.roundButton = roundButton;
return this;
}
/**
* set a fast listener ( it shows a dialog without buttons and the event fires as soon you select a color )
* @param listener
* @return
*/
public ColorPicker setFastChooser(OnFastChooseColorListener listener){
this.fastChooser = true;
this.onFastChooseColorListener = listener;
return this;
}
/**
* set a listener for the color picked
* @param listener
*/
public void setOnChooseColorListener(OnChooseColorListener listener) {
onChooseColorListener = listener;
}
/**
* set if to dismiss the dialog or not on button click, by default is set to true
*/
public ColorPicker setDismissOnButtonClick(boolean dismiss){
this.dismiss = dismiss;
return this;
}
/**
* set Match_parent to RecyclerView
* @return
*/
public ColorPicker setDialogFullHeight(){
this.fullheight = true;
return this;
}
/**
* Choose the color to be selected by default
* @param color int
* @return
*/
public ColorPicker setDefaultColor(int color){
this.default_color = color;
return this;
}
/**
* getDialog if you need more options
* @return
*/
public MaterialDialog getDialog(){
return mMaterialDialog;
}
/**
* dismiss the dialog
*/
public void dismissDialog(){
if(mMaterialDialog != null ) {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
mMaterialDialog.dismiss();
}
};
handler.postDelayed(runnable,250);
}
}
private ColorPicker setTitlePadding(int left, int top, int right, int bottom) {
paddingTitleLeft = left;
paddingTitleRight = right;
paddingTitleTop = top;
paddingTitleBottom = bottom;
return this;
}
private int dip2px(float dpValue) {
final float scale = activity.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}