Skip to content

Commit 23270f2

Browse files
committed
Image position and text margin bug fix. Small refactor.
1 parent faedf4a commit 23270f2

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

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

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,8 @@ public CustomButton(Context context, AttributeSet attrs) {
208208
textColorDisabled = textColorNormal;
209209
}
210210

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;
216213
}
217214

218215
if (drawableColorStateListAttr != null)
@@ -314,42 +311,7 @@ private void setContent() {
314311
if (drawableDisabled == null)
315312
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
316313

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();
353315

354316
} else {
355317
layoutParamsText = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
@@ -365,6 +327,47 @@ private void setContent() {
365327
updateText();
366328
}
367329

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+
368371
/**
369372
* Set shape backgrounds to button. Used global backgroundColorNormal variables.
370373
*/
@@ -565,15 +568,15 @@ public void onGlobalLayout() {
565568
scale = ((float) container.getHeight()) / ((float) drawableNormal.getIntrinsicHeight());
566569
layoutParams.width = (int) (scale * drawableNormal.getIntrinsicWidth());
567570
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) {
573572
if (drawablePosition == LEFT)
574573
((LayoutParams) textView.getLayoutParams()).rightMargin = layoutParams.width;
575574
else
576575
((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;
577580
}
578581
if (text == null) {
579582
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
@@ -945,7 +948,7 @@ public void setImageColors(int normal, int pressed, int disabled) {
945948
this.drawableColorDisabled = disabled;
946949

947950
setImageColorStateList();
948-
setContent();
951+
updateContent();
949952
}
950953

951954
/**
@@ -960,25 +963,25 @@ public void setImageColors(int normal, int pressed, int disabled) {
960963
public void setImageColors(ColorStateList colorStateList) {
961964
imageColorStateListToIntegers(colorStateList);
962965
setImageColorStateList();
963-
setContent();
966+
updateContent();
964967
}
965968

966969
public void setImageNormalColor(int color) {
967970
this.drawableColorNormal = color;
968971
setImageColorStateList();
969-
setContent();
972+
updateContent();
970973
}
971974

972975
public void setImagePressedColor(int color) {
973976
this.drawableColorPressed = color;
974977
setImageColorStateList();
975-
setContent();
978+
updateContent();
976979
}
977980

978981
public void setImageDisableColor(int color) {
979982
this.drawableColorDisabled = color;
980983
setImageColorStateList();
981-
setContent();
984+
updateContent();
982985
}
983986

984987
/**

0 commit comments

Comments
 (0)