@@ -33,13 +33,14 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
33
33
private final int COLOR_INDEX_PRESSED = 0 ;
34
34
private final int COLOR_INDEX_NORMAL = 1 ;
35
35
private final int COLOR_INDEX_DISABLED = 2 ;
36
- private final int [][] stateArray = new int [][]{new int []{android .R .attr .state_pressed }, new int []{android .R .attr .state_enabled }, new int []{}}; //array for support states
36
+ private final int [][] stateArray = new int [][]{new int []{android .R .attr .state_pressed }, new int []{android .R .attr .state_enabled }, new int []{}}; //array for support states DON'T TOUCH THIS
37
37
38
38
protected TextView textView ; //text container
39
39
protected LinearLayout container ; //all content container
40
40
protected ImageView imageContainer ;
41
41
42
42
private int primaryColor , secondaryColor ;
43
+ private int primaryColorLight , secondaryColorLight ;
43
44
private int backgroundColorPressed , backgroundColorDisabled , backgroundColorNormal ; // colors for each backgroundColorNormal state
44
45
private ColorStateList backgroundColorState ; // color state list for backgroundColorNormal
45
46
private int textColorPressed , textColorDisabled , textColorNormal ; // colors for each text color state
@@ -86,17 +87,8 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryC
86
87
87
88
this .drawableNormal = imageNormal ;
88
89
89
- this .backgroundColorNormal = primaryColor ;
90
- this .backgroundColorPressed = secondaryColor ;
91
- this .backgroundColorDisabled = secondaryColor ;
92
-
93
- this .textColorNormal = secondaryColor ;
94
- this .textColorDisabled = primaryColor ;
95
- this .textColorPressed = primaryColor ;
96
-
97
- this .frameColorNormal = secondaryColor ;
98
- this .frameColorPressed = primaryColor ;
99
- this .frameColorDisabled = primaryColor ;
90
+ updatePrimaryColor (primaryColor );
91
+ updateSecondaryColor (secondaryColor );
100
92
101
93
this .shapeType = 0 ;
102
94
this .shapeRadius = 0 ;
@@ -127,12 +119,12 @@ public CustomButton(Context context, AttributeSet attrs) {
127
119
128
120
backgroundColorNormal = attributes .getColor (R .styleable .CustomButton_cb_background , primaryColor );
129
121
backgroundColorPressed = attributes .getColor (R .styleable .CustomButton_cb_background_pressed , secondaryColor );
130
- backgroundColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_background_disabled , secondaryColor );
122
+ backgroundColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_background_disabled , lighterColor ( primaryColor ) );
131
123
backgroundColorState = attributes .getColorStateList (R .styleable .CustomButton_cb_background_state_list );
132
124
133
125
textColorNormal = attributes .getColor (R .styleable .CustomButton_cb_text_color , secondaryColor );
134
126
textColorPressed = attributes .getColor (R .styleable .CustomButton_cb_text_color_pressed , primaryColor );
135
- textColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_text_color_disabled , primaryColor );
127
+ textColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_text_color_disabled , lighterColor ( secondaryColor ) );
136
128
textColorState = attributes .getColorStateList (R .styleable .CustomButton_android_textColor );
137
129
textSize = attributes .getDimension (R .styleable .CustomButton_cb_text_size , 0 );
138
130
text = attributes .getString (R .styleable .CustomButton_android_text );
@@ -147,7 +139,7 @@ public CustomButton(Context context, AttributeSet attrs) {
147
139
shapeTypeAttr = attributes .getInt (R .styleable .CustomButton_cb_shape_type , 0 );
148
140
frameColorNormal = attributes .getColor (R .styleable .CustomButton_cb_frame_color , secondaryColor );
149
141
frameColorPressed = attributes .getColor (R .styleable .CustomButton_cb_frame_color_pressed , primaryColor );
150
- frameColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_frame_color_disabled , primaryColor );
142
+ frameColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_frame_color_disabled , lighterColor ( secondaryColor ) );
151
143
frameColorState = attributes .getColorStateList (R .styleable .CustomButton_cb_frame_state_list );
152
144
frameSize = attributes .getDimension (R .styleable .CustomButton_cb_frame_size , 0 );
153
145
@@ -402,26 +394,36 @@ private void updateText() {
402
394
private void updatePrimaryColor (int color ) {
403
395
404
396
this .primaryColor = color ;
397
+ this .primaryColorLight = lighterColor (color );
405
398
406
399
this .backgroundColorNormal = color ;
407
-
408
- this .textColorDisabled = color ;
409
400
this .textColorPressed = color ;
410
-
411
401
this .frameColorPressed = color ;
412
- this .frameColorDisabled = color ;
402
+ this .textColorDisabled = primaryColorLight ;
403
+ this .frameColorDisabled = primaryColorLight ;
413
404
}
414
405
415
406
private void updateSecondaryColor (int color ) {
416
407
417
408
this .secondaryColor = color ;
409
+ this .secondaryColorLight = lighterColor (color );
418
410
419
411
this .backgroundColorPressed = color ;
420
- this .backgroundColorDisabled = color ;
421
-
422
412
this .textColorNormal = color ;
423
-
424
413
this .frameColorNormal = color ;
414
+ this .backgroundColorDisabled = secondaryColorLight ;
415
+ }
416
+
417
+ private int lighterColor (int color ) {
418
+
419
+ int redPC = Color .red (color );
420
+ int greenPC = Color .green (color );
421
+ int blueRC = Color .blue (color );
422
+ float hsv [] = new float [3 ];
423
+
424
+ Color .RGBToHSV (redPC , greenPC , blueRC , hsv );
425
+ hsv [1 ] = (hsv [1 ] - 0.3f ) < 0 ? 0.1f : hsv [1 ] - 0.3f ;
426
+ return Color .HSVToColor (hsv );
425
427
}
426
428
427
429
0 commit comments