Skip to content

Commit 94baa91

Browse files
committed
Small bug fix and refactor
1 parent 55cf12e commit 94baa91

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

library/src/main/java/pl/sigmapoint/customview/CustomButton.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void setContent() {
267267

268268
LayoutParams layoutParamsText;
269269
LayoutParams layoutParamsImage;
270-
if (drawablePosition >= 0 || drawablePosition <= 3 || drawableNormal != null) {
270+
if (drawablePosition >= 0 || drawablePosition <= 3 && drawableNormal != null) {
271271
int width = (drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT;
272272
int height = (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0;
273273
layoutParamsText = new LinearLayout.LayoutParams(width, height);
@@ -279,8 +279,10 @@ private void setContent() {
279279
textView.setLayoutParams(layoutParamsText);
280280
imageContainer.setLayoutParams(layoutParamsImage);
281281

282-
if (drawablePressed == null) drawablePressed = drawableNormal;
283-
if (drawableDisabled == null) drawablePressed = drawableNormal;
282+
if (drawablePressed == null)
283+
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
284+
if (drawableDisabled == null)
285+
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
284286

285287
StateListDrawable listDrawable = new StateListDrawable();
286288
listDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);
@@ -320,7 +322,7 @@ private void setContent() {
320322
}
321323

322324
} else {
323-
layoutParamsText = new LinearLayout.LayoutParams(getLayoutParams().width, getLayoutParams().height);
325+
layoutParamsText = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
324326
textView.setLayoutParams(layoutParamsText);
325327
if (text != null) {
326328
textView.setGravity(Gravity.CENTER);
@@ -420,7 +422,6 @@ private void imageColorStateListToIntegers(ColorStateList colorStateList) {
420422
drawableColorDisabled = globalColor;
421423
}
422424

423-
424425
private void updateText() {
425426
textColorArray = new int[]{textColorPressed, textColorNormal, textColorDisabled};
426427
textColorList = new ColorStateList(stateArray, textColorArray);
@@ -500,7 +501,6 @@ private Drawable changeDrawableColor(Bitmap bitmap, int color) {
500501
return new BitmapDrawable(b);
501502
}
502503

503-
504504
/**
505505
* Set shape backgroundColorNormal.
506506
*
@@ -513,7 +513,6 @@ public void setShapeBackground(int shapeType, int shapeRadius) {
513513
setShapeBackground();
514514
}
515515

516-
517516
public void setPrimaryColor(int color) {
518517

519518
updatePrimaryColor(color);
@@ -548,7 +547,6 @@ public void setMainColors(int primaryColor, int secondaryColor) {
548547
* pressed - for pressed state,
549548
* (empty) - for disabled state.
550549
*/
551-
552550
public void setBackgroundColorStateList(ColorStateList colorStateList) {
553551

554552
backgroundColorStateListToIntegers(colorStateList);
@@ -583,7 +581,6 @@ public void setBackgroundColorNormal(int backgroundColorNormal) {
583581
* @param color Integer frame color
584582
* @param size Float frame size
585583
*/
586-
587584
public void setFrame(int color, float size) {
588585
frameSize = size;
589586
frameColorNormal = color;
@@ -752,6 +749,13 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
752749
this.imageWeight = weight;
753750
this.imagePaddingArray = padding;
754751

752+
if (padding != null)
753+
imageContainer.setPadding(padding[LEFT], padding[TOP], padding[RIGHT], padding[BOTTOM]);
754+
else
755+
imageContainer.setPadding(0, 0, 0, 0);
756+
if (scaleType != null)
757+
imageContainer.setScaleType(scaleType);
758+
755759
setContent();
756760
}
757761

@@ -764,19 +768,7 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
764768
* @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
765769
*/
766770
public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleType, int weight, int[] padding) {
767-
this.drawableNormal = drawable;
768-
this.drawablePosition = position;
769-
this.imageWeight = weight;
770-
this.imagePaddingArray = padding;
771-
772-
if (padding != null)
773-
imageContainer.setPadding(padding[LEFT], padding[TOP], padding[RIGHT], padding[BOTTOM]);
774-
else
775-
imageContainer.setPadding(0, 0, 0, 0);
776-
if (scaleType != null)
777-
imageContainer.setScaleType(scaleType);
778-
779-
setContent();
771+
setImage(position, drawable, drawable, drawable, scaleType, weight, padding);
780772
}
781773

782774
public void setImageColors(int normal, int pressed, int disabled) {
@@ -812,7 +804,6 @@ public void setImageDisableColor(int color) {
812804
setContent();
813805
}
814806

815-
816807
/**
817808
* Set elevation to button. If enabled button is smaller because shadow must have space to show.
818809
*
@@ -853,7 +844,6 @@ public int getShapeType() {
853844
return shapeType;
854845
}
855846

856-
857847
public boolean isElevationEnabled() {
858848
return isElevationEnabled;
859849
}

0 commit comments

Comments
 (0)