@@ -55,15 +55,15 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
55
55
private int textColorPressed , textColorDisabled , textColorNormal ; // colors for each text color state
56
56
private ColorStateList textColorState ; // color state list for text
57
57
private float textSize ;
58
- private String text ; // text inside button
58
+ private String text ;
59
59
private int textPadding ;
60
60
private int [] textPaddingArray ;
61
61
private int textWeight ;
62
62
private float shapeRadius ; // corner radius
63
63
private int shapeTypeAttr ; // shape type (from xml - converted to shape type constants)
64
64
private int frameColorPressed , frameColorDisabled , frameColorNormal ; // colors for each frame color state
65
65
private ColorStateList frameColorState ;
66
- private float frameSize ; // frame size
66
+ private float frameSize ;
67
67
private boolean isElevationEnabled ; // is elevation should be displayed >=API 21
68
68
private Drawable drawableNormal , drawableDisabled , drawablePressed ;
69
69
private int drawableColorNormal , drawableColorDisabled , drawableColorPressed ;
@@ -86,8 +86,17 @@ private void init(Context context) {
86
86
container = new LinearLayout (context );
87
87
textView = new TextView (context );
88
88
imageContainer = new ImageView (context );
89
+
90
+ setGravity (Gravity .TOP );
89
91
}
90
92
93
+ /**
94
+ * @param context application context
95
+ * @param params layout parameters (width, height, weight, etc.)
96
+ * @param primaryColor primary color of button
97
+ * @param secondaryColor secondary color of button
98
+ * @param imageNormal can be null if button doesn't have image
99
+ */
91
100
public CustomButton (Context context , ViewGroup .LayoutParams params , int primaryColor , int secondaryColor , Drawable imageNormal ) {
92
101
super (context );
93
102
init (context );
@@ -117,6 +126,12 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryC
117
126
setOnClickListener (this );
118
127
}
119
128
129
+ /**
130
+ * If you use it, you must specify all parameters from setters.
131
+ *
132
+ * @param context
133
+ * @param attrs
134
+ */
120
135
public CustomButton (Context context , AttributeSet attrs ) {
121
136
super (context , attrs );
122
137
@@ -183,24 +198,27 @@ public CustomButton(Context context, AttributeSet attrs) {
183
198
frameColorStateListToIntegers (frameColorState );
184
199
}
185
200
201
+ if (backgroundColorNormal != primaryColor && backgroundColorPressed == secondaryColor && backgroundColorDisabled == lighterColor (primaryColor )) { //if only normal color is specified
202
+ backgroundColorPressed = backgroundColorNormal ;
203
+ backgroundColorDisabled = backgroundColorNormal ;
204
+ }
205
+
206
+ if (textColorNormal != secondaryColor && textColorPressed == primaryColor && textColorDisabled == lighterColor (secondaryColor )) { //if only normal color is specified
207
+ textColorPressed = textColorNormal ;
208
+ textColorDisabled = textColorNormal ;
209
+ }
210
+
211
+ if (drawableNormal != null ) { //if only normal image is specified
212
+ if (drawablePressed == null )
213
+ drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
214
+ if (drawableDisabled == null )
215
+ drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
216
+ }
217
+
186
218
if (drawableColorStateListAttr != null )
187
219
imageColorStateListToIntegers (drawableColorStateListAttr );
188
- setImageColorStateList ();
189
- if (drawableNormal != null ) {
190
- if (drawableColorNormal != Integer .MAX_VALUE ) {
191
- drawableNormal = changeDrawableColor (((BitmapDrawable ) drawableNormal ).getBitmap (), drawableColorNormal );
192
- }
193
- if (drawableColorPressed != Integer .MAX_VALUE ) {
194
- if (drawablePressed == null )
195
- drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
196
- drawablePressed = changeDrawableColor (((BitmapDrawable ) drawablePressed ).getBitmap (), drawableColorPressed );
197
- }
198
- if (drawableColorDisabled != Integer .MAX_VALUE ) {
199
- if (drawableDisabled == null )
200
- drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
201
- drawableDisabled = changeDrawableColor (((BitmapDrawable ) drawableDisabled ).getBitmap (), drawableColorDisabled );
202
- }
203
- }
220
+
221
+ setImageColorStateList (); //works only for images, don't work for shape, etc.
204
222
205
223
//WARNING: If you want to change it, you should change it in attr xml too
206
224
switch (imageScaleTypeAttr ) {
@@ -424,7 +442,14 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
424
442
frameColorNormal = colorStateList .getColorForState (new int []{android .R .attr .state_enabled }, globalColor );
425
443
frameColorDisabled = globalColor ;
426
444
}
427
-
445
+ /**
446
+ * Convert color state list to three integers. It is helper method to set image color.
447
+ *
448
+ * @param colorStateList is a frame color state list which should be converted. Should have three states:
449
+ * enabled - for normal state,
450
+ * pressed - for pressed state,
451
+ * (empty) - for disabled state.
452
+ */
428
453
private void imageColorStateListToIntegers (ColorStateList colorStateList ) {
429
454
430
455
int globalColor = colorStateList .getColorForState (new int []{}, 0 );
@@ -434,6 +459,9 @@ private void imageColorStateListToIntegers(ColorStateList colorStateList) {
434
459
drawableColorDisabled = globalColor ;
435
460
}
436
461
462
+ /**
463
+ * Update text, color and size. Helper method.
464
+ */
437
465
private void updateText () {
438
466
textColorArray = new int []{textColorPressed , textColorNormal , textColorDisabled };
439
467
textColorList = new ColorStateList (stateArray , textColorArray );
@@ -485,17 +513,18 @@ private int lighterColor(int color) {
485
513
486
514
private void setImageColorStateList () {
487
515
if (drawableNormal != null ) {
516
+ if (drawablePressed == null )
517
+ drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
518
+ if (drawableDisabled == null )
519
+ drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
520
+
488
521
if (drawableColorNormal != Integer .MAX_VALUE ) {
489
522
drawableNormal = changeDrawableColor (((BitmapDrawable ) drawableNormal ).getBitmap (), drawableColorNormal );
490
523
}
491
524
if (drawableColorPressed != Integer .MAX_VALUE ) {
492
- if (drawablePressed == null )
493
- drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
494
525
drawablePressed = changeDrawableColor (((BitmapDrawable ) drawablePressed ).getBitmap (), drawableColorPressed );
495
526
}
496
527
if (drawableColorDisabled != Integer .MAX_VALUE ) {
497
- if (drawableDisabled == null )
498
- drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
499
528
drawableDisabled = changeDrawableColor (((BitmapDrawable ) drawableDisabled ).getBitmap (), drawableColorDisabled );
500
529
}
501
530
}
@@ -626,6 +655,16 @@ public void setBackgroundColorDisabled(int backgroundColorDisabled) {
626
655
627
656
public void setBackgroundColorNormal (int backgroundColorNormal ) {
628
657
this .backgroundColorNormal = backgroundColorNormal ;
658
+
659
+ if (backgroundColorPressed == 0 && backgroundColorDisabled == 0 ) {
660
+ backgroundColorPressed = backgroundColorNormal ;
661
+ backgroundColorDisabled = backgroundColorNormal ;
662
+ }
663
+
664
+ if (textColorPressed == 0 && textColorDisabled == 0 ) {
665
+ textColorPressed = textColorNormal ;
666
+ textColorDisabled = textColorNormal ;
667
+ }
629
668
setShapeBackground ();
630
669
}
631
670
@@ -873,7 +912,7 @@ public void setElevationEnabled(boolean enabled) {
873
912
int var ;
874
913
875
914
if (enabled ) {
876
- var = 6 ;
915
+ var = 3 ;
877
916
container .setStateListAnimator (AnimatorInflater .loadStateListAnimator (getContext (), R .anim .elevation_button_custom ));
878
917
} else {
879
918
var = 0 ;
0 commit comments