@@ -208,11 +208,8 @@ public CustomButton(Context context, AttributeSet attrs) {
208
208
textColorDisabled = textColorNormal ;
209
209
}
210
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 ();
211
+ if (drawableNormal != null && drawablePosition == -1 ) {
212
+ drawablePosition = LEFT ;
216
213
}
217
214
218
215
if (drawableColorStateListAttr != null )
@@ -314,42 +311,7 @@ private void setContent() {
314
311
if (drawableDisabled == null )
315
312
drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
316
313
317
- StateListDrawable listDrawable = new StateListDrawable ();
318
- listDrawable .addState (new int []{android .R .attr .state_pressed }, drawablePressed );
319
- listDrawable .addState (new int []{android .R .attr .state_enabled }, drawableNormal );
320
- listDrawable .addState (new int []{}, drawableDisabled );
321
-
322
- if (imagePaddingArray != null )
323
- imageContainer .setPadding (imagePaddingArray [LEFT ], imagePaddingArray [TOP ], imagePaddingArray [RIGHT ], imagePaddingArray [BOTTOM ]);
324
- imageContainer .setScaleType (imageScaleType );
325
-
326
- imageContainer .setImageDrawable (listDrawable );
327
-
328
- switch (drawablePosition ) {
329
- case LEFT :
330
- container .setOrientation (HORIZONTAL );
331
- container .addView (imageContainer );
332
- break ;
333
- case RIGHT :
334
- container .setOrientation (HORIZONTAL );
335
- break ;
336
- case TOP :
337
- container .addView (imageContainer );
338
- break ;
339
- }
340
- if (text != null ) {
341
- textView .setGravity (Gravity .CENTER );
342
- container .addView (textView );
343
- }
344
-
345
- switch (drawablePosition ) {
346
- case RIGHT :
347
- container .addView (imageContainer );
348
- break ;
349
- case BOTTOM :
350
- container .addView (imageContainer );
351
- break ;
352
- }
314
+ updateContent ();
353
315
354
316
} else {
355
317
layoutParamsText = new LinearLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT );
@@ -365,6 +327,47 @@ private void setContent() {
365
327
updateText ();
366
328
}
367
329
330
+ private void updateContent () {
331
+ container .removeAllViews ();
332
+
333
+ StateListDrawable listDrawable = new StateListDrawable ();
334
+ listDrawable .addState (new int []{android .R .attr .state_pressed }, drawablePressed );
335
+ listDrawable .addState (new int []{android .R .attr .state_enabled }, drawableNormal );
336
+ listDrawable .addState (new int []{}, drawableDisabled );
337
+
338
+ if (imagePaddingArray != null )
339
+ imageContainer .setPadding (imagePaddingArray [LEFT ], imagePaddingArray [TOP ], imagePaddingArray [RIGHT ], imagePaddingArray [BOTTOM ]);
340
+ imageContainer .setScaleType (imageScaleType );
341
+
342
+ imageContainer .setImageDrawable (listDrawable );
343
+
344
+ switch (drawablePosition ) {
345
+ case LEFT :
346
+ container .setOrientation (HORIZONTAL );
347
+ container .addView (imageContainer );
348
+ break ;
349
+ case RIGHT :
350
+ container .setOrientation (HORIZONTAL );
351
+ break ;
352
+ case TOP :
353
+ container .addView (imageContainer );
354
+ break ;
355
+ }
356
+ if (text != null ) {
357
+ textView .setGravity (Gravity .CENTER );
358
+ container .addView (textView );
359
+ }
360
+
361
+ switch (drawablePosition ) {
362
+ case RIGHT :
363
+ container .addView (imageContainer );
364
+ break ;
365
+ case BOTTOM :
366
+ container .addView (imageContainer );
367
+ break ;
368
+ }
369
+ }
370
+
368
371
/**
369
372
* Set shape backgrounds to button. Used global backgroundColorNormal variables.
370
373
*/
@@ -565,15 +568,15 @@ public void onGlobalLayout() {
565
568
scale = ((float ) container .getHeight ()) / ((float ) drawableNormal .getIntrinsicHeight ());
566
569
layoutParams .width = (int ) (scale * drawableNormal .getIntrinsicWidth ());
567
570
layoutParams .height = ViewGroup .LayoutParams .MATCH_PARENT ;
568
- if (layoutParams .width > container .getWidth () / 2 ) {
569
- layoutParams .width = 0 ;
570
- layoutParams .weight = 1 ;
571
- imageWeight = 1 ;
572
- } else {
571
+ if (layoutParams .width + textView .getPaint ().measureText (textView .getText ().toString ()) < container .getWidth () / 2 ) {
573
572
if (drawablePosition == LEFT )
574
573
((LayoutParams ) textView .getLayoutParams ()).rightMargin = layoutParams .width ;
575
574
else
576
575
((LayoutParams ) textView .getLayoutParams ()).leftMargin = layoutParams .width ;
576
+ } else if (layoutParams .width > container .getWidth () / 2 ) {
577
+ layoutParams .width = 0 ;
578
+ layoutParams .weight = 1 ;
579
+ imageWeight = 1 ;
577
580
}
578
581
if (text == null ) {
579
582
layoutParams .height = ViewGroup .LayoutParams .MATCH_PARENT ;
@@ -945,7 +948,7 @@ public void setImageColors(int normal, int pressed, int disabled) {
945
948
this .drawableColorDisabled = disabled ;
946
949
947
950
setImageColorStateList ();
948
- setContent ();
951
+ updateContent ();
949
952
}
950
953
951
954
/**
@@ -960,25 +963,25 @@ public void setImageColors(int normal, int pressed, int disabled) {
960
963
public void setImageColors (ColorStateList colorStateList ) {
961
964
imageColorStateListToIntegers (colorStateList );
962
965
setImageColorStateList ();
963
- setContent ();
966
+ updateContent ();
964
967
}
965
968
966
969
public void setImageNormalColor (int color ) {
967
970
this .drawableColorNormal = color ;
968
971
setImageColorStateList ();
969
- setContent ();
972
+ updateContent ();
970
973
}
971
974
972
975
public void setImagePressedColor (int color ) {
973
976
this .drawableColorPressed = color ;
974
977
setImageColorStateList ();
975
- setContent ();
978
+ updateContent ();
976
979
}
977
980
978
981
public void setImageDisableColor (int color ) {
979
982
this .drawableColorDisabled = color ;
980
983
setImageColorStateList ();
981
- setContent ();
984
+ updateContent ();
982
985
}
983
986
984
987
/**
0 commit comments