4
4
import android .content .Context ;
5
5
import android .content .res .ColorStateList ;
6
6
import android .content .res .TypedArray ;
7
+ import android .graphics .Color ;
7
8
import android .graphics .drawable .Drawable ;
8
9
import android .graphics .drawable .GradientDrawable ;
9
10
import android .graphics .drawable .RippleDrawable ;
@@ -38,6 +39,7 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
38
39
protected LinearLayout container ; //all content container
39
40
protected ImageView imageContainer ;
40
41
42
+ private int primaryColor , secondaryColor ;
41
43
private int backgroundColorPressed , backgroundColorDisabled , backgroundColorNormal ; // colors for each backgroundColorNormal state
42
44
private ColorStateList backgroundColorState ; // color state list for backgroundColorNormal
43
45
private int textColorPressed , textColorDisabled , textColorNormal ; // colors for each text color state
@@ -74,29 +76,36 @@ private void init(Context context) {
74
76
imageContainer = new ImageView (context );
75
77
}
76
78
77
- public CustomButton (Context context , ViewGroup .LayoutParams params , int backgroundColorNormal , int textColorNormal , Drawable imageNormal ) {
79
+ public CustomButton (Context context , ViewGroup .LayoutParams params , int primaryColor , int secondaryColor , Drawable imageNormal ) {
78
80
super (context );
79
81
init (context );
80
82
setLayoutParams (params );
81
83
82
- this .backgroundColorNormal = backgroundColorNormal ;
83
- this .textColorNormal = textColorNormal ;
84
+ this .primaryColor = primaryColor ;
85
+ this .secondaryColor = secondaryColor ;
86
+
84
87
this .drawableNormal = imageNormal ;
85
88
86
- this .backgroundColorPressed = backgroundColorNormal ;
87
- this .backgroundColorDisabled = backgroundColorNormal ;
89
+ this .backgroundColorNormal = primaryColor ;
90
+ this .backgroundColorPressed = secondaryColor ;
91
+ this .backgroundColorDisabled = secondaryColor ;
88
92
89
- this .textColorDisabled = textColorNormal ;
90
- this .textColorPressed = textColorNormal ;
93
+ this .textColorNormal = secondaryColor ;
94
+ this .textColorDisabled = primaryColor ;
95
+ this .textColorPressed = primaryColor ;
91
96
92
- shapeType = 0 ;
93
- shapeRadius = 0 ;
94
- imageScaleType = ImageView . ScaleType . FIT_CENTER ;
97
+ this . frameColorNormal = secondaryColor ;
98
+ this . frameColorPressed = primaryColor ;
99
+ this . frameColorDisabled = primaryColor ;
95
100
96
- if (drawableNormal != null ) drawablePosition = LEFT ; //default position
101
+ this .shapeType = 0 ;
102
+ this .shapeRadius = 0 ;
103
+ this .imageScaleType = ImageView .ScaleType .FIT_CENTER ;
104
+
105
+ if (drawableNormal != null ) this .drawablePosition = LEFT ; //default position
97
106
else drawablePosition = -1 ;
98
107
99
- setContent (context );
108
+ setContent ();
100
109
101
110
setShapeBackground (); // set shape and backgroundColorNormal to button
102
111
setElevationEnabled (true ); // this method will work only for post-L android. Set elevation or disable it and set margins if needed
@@ -112,14 +121,18 @@ public CustomButton(Context context, AttributeSet attrs) {
112
121
TypedArray attributes = context .getTheme ().obtainStyledAttributes (attrs , R .styleable .CustomButton , 0 , 0 );
113
122
114
123
try {
115
- backgroundColorNormal = attributes .getColor (R .styleable .CustomButton_cb_background , 0 );
116
- backgroundColorPressed = attributes .getColor (R .styleable .CustomButton_cb_background_pressed , backgroundColorNormal );
117
- backgroundColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_background_disabled , backgroundColorNormal );
124
+
125
+ primaryColor = attributes .getColor (R .styleable .CustomButton_cb_primary_color , Color .LTGRAY );
126
+ secondaryColor = attributes .getColor (R .styleable .CustomButton_cb_secondary_color , Color .GRAY );
127
+
128
+ backgroundColorNormal = attributes .getColor (R .styleable .CustomButton_cb_background , primaryColor );
129
+ backgroundColorPressed = attributes .getColor (R .styleable .CustomButton_cb_background_pressed , secondaryColor );
130
+ backgroundColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_background_disabled , secondaryColor );
118
131
backgroundColorState = attributes .getColorStateList (R .styleable .CustomButton_cb_background_state_list );
119
132
120
- textColorNormal = attributes .getColor (R .styleable .CustomButton_cb_text_color , 0 );
121
- textColorPressed = attributes .getColor (R .styleable .CustomButton_cb_text_color_pressed , textColorNormal );
122
- textColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_text_color_disabled , textColorNormal );
133
+ textColorNormal = attributes .getColor (R .styleable .CustomButton_cb_text_color , secondaryColor );
134
+ textColorPressed = attributes .getColor (R .styleable .CustomButton_cb_text_color_pressed , primaryColor );
135
+ textColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_text_color_disabled , primaryColor );
123
136
textColorState = attributes .getColorStateList (R .styleable .CustomButton_android_textColor );
124
137
textSize = attributes .getDimension (R .styleable .CustomButton_cb_text_size , 0 );
125
138
text = attributes .getString (R .styleable .CustomButton_android_text );
@@ -132,9 +145,9 @@ public CustomButton(Context context, AttributeSet attrs) {
132
145
133
146
shapeRadius = attributes .getDimension (R .styleable .CustomButton_cb_shape_radius , 0 );
134
147
shapeTypeAttr = attributes .getInt (R .styleable .CustomButton_cb_shape_type , 0 );
135
- frameColorNormal = attributes .getColor (R .styleable .CustomButton_cb_frame_color , 0 );
136
- frameColorPressed = attributes .getColor (R .styleable .CustomButton_cb_frame_color_pressed , frameColorNormal );
137
- frameColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_frame_color_disabled , frameColorNormal );
148
+ frameColorNormal = attributes .getColor (R .styleable .CustomButton_cb_frame_color , secondaryColor );
149
+ frameColorPressed = attributes .getColor (R .styleable .CustomButton_cb_frame_color_pressed , primaryColor );
150
+ frameColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_frame_color_disabled , primaryColor );
138
151
frameColorState = attributes .getColorStateList (R .styleable .CustomButton_cb_frame_state_list );
139
152
frameSize = attributes .getDimension (R .styleable .CustomButton_cb_frame_size , 0 );
140
153
@@ -187,7 +200,7 @@ public CustomButton(Context context, AttributeSet attrs) {
187
200
break ;
188
201
}
189
202
190
- setContent (context );
203
+ setContent ();
191
204
192
205
shapeType = (shapeTypeAttr == 0 ) ? GradientDrawable .RECTANGLE : GradientDrawable .OVAL ; // convert xml attributes value to shape type constant
193
206
@@ -213,7 +226,7 @@ public void setEnabled(boolean enabled) {
213
226
container .setEnabled (enabled );
214
227
}
215
228
216
- private void setContent (Context context ) {
229
+ private void setContent () {
217
230
removeAllViews ();
218
231
container .removeAllViews ();
219
232
@@ -291,15 +304,7 @@ private void setContent(Context context) {
291
304
292
305
addView (container );
293
306
294
- textColorArray = new int []{textColorPressed , textColorNormal , textColorDisabled };
295
- textColorList = new ColorStateList (stateArray , textColorArray );
296
-
297
- if (text != null ) {
298
- textView .setText (text );
299
- if (textSize > 0 ) textView .setTextSize (textSize );
300
- if (textColorNormal != 0 ) textView .setTextColor (textColorList );
301
- else if (textColorState != null ) textView .setTextColor (textColorState );
302
- }
307
+ updateText ();
303
308
}
304
309
305
310
/**
@@ -380,6 +385,46 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
380
385
frameColorDisabled = globalColor ;
381
386
}
382
387
388
+ private void updateText () {
389
+ textColorArray = new int []{textColorPressed , textColorNormal , textColorDisabled };
390
+ textColorList = new ColorStateList (stateArray , textColorArray );
391
+
392
+ if (text != null ) {
393
+ textView .setText (text );
394
+ if (textSize > 0 ) textView .setTextSize (textSize );
395
+ if (textColorNormal != 0 ) textView .setTextColor (textColorList );
396
+ else if (textColorState != null ) textView .setTextColor (textColorState );
397
+ } else {
398
+ container .removeView (textView );
399
+ }
400
+ }
401
+
402
+ private void updatePrimaryColor (int color ) {
403
+
404
+ this .primaryColor = color ;
405
+
406
+ this .backgroundColorNormal = color ;
407
+
408
+ this .textColorDisabled = color ;
409
+ this .textColorPressed = color ;
410
+
411
+ this .frameColorPressed = color ;
412
+ this .frameColorDisabled = color ;
413
+ }
414
+
415
+ private void updateSecondaryColor (int color ) {
416
+
417
+ this .secondaryColor = color ;
418
+
419
+ this .backgroundColorPressed = color ;
420
+ this .backgroundColorDisabled = color ;
421
+
422
+ this .textColorNormal = color ;
423
+
424
+ this .frameColorNormal = color ;
425
+ }
426
+
427
+
383
428
/**
384
429
* Set shape backgroundColorNormal.
385
430
*
@@ -392,6 +437,32 @@ public void setShapeBackground(int shapeType, int shapeRadius) {
392
437
setShapeBackground ();
393
438
}
394
439
440
+
441
+ public void setPrimaryColor (int color ) {
442
+
443
+ updatePrimaryColor (color );
444
+
445
+ updateText ();
446
+ setShapeBackground ();
447
+ }
448
+
449
+ public void setSecondaryColor (int color ) {
450
+
451
+ updateSecondaryColor (color );
452
+
453
+ updateText ();
454
+ setShapeBackground ();
455
+ }
456
+
457
+ public void setMainColors (int primaryColor , int secondaryColor ) {
458
+
459
+ updatePrimaryColor (primaryColor );
460
+ updateSecondaryColor (secondaryColor );
461
+
462
+ updateText ();
463
+ setShapeBackground ();
464
+ }
465
+
395
466
/**
396
467
* Set background color from color state list.
397
468
* Only three states are use: disabled, pressed, normal.
@@ -507,7 +578,7 @@ public void setText(String text) {
507
578
this .text = text ;
508
579
textView .setText (text );
509
580
if (textView .getParent () != container ) {
510
- setContent (getContext () );
581
+ setContent ();
511
582
}
512
583
}
513
584
@@ -605,7 +676,7 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
605
676
this .imageWeight = weight ;
606
677
this .imagePaddingArray = padding ;
607
678
608
- setContent (getContext () );
679
+ setContent ();
609
680
}
610
681
611
682
/**
@@ -629,7 +700,7 @@ public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleT
629
700
if (scaleType != null )
630
701
imageContainer .setScaleType (scaleType );
631
702
632
- setContent (getContext () );
703
+ setContent ();
633
704
}
634
705
635
706
/**
@@ -797,4 +868,11 @@ public int getTextWeight() {
797
868
return textWeight ;
798
869
}
799
870
871
+ public int getPrimaryColor () {
872
+ return primaryColor ;
873
+ }
874
+
875
+ public int getSecondaryColor () {
876
+ return secondaryColor ;
877
+ }
800
878
}
0 commit comments