Skip to content

Instantly share code, notes, and snippets.

@androidcodehunter
Created November 12, 2016 12:19
Show Gist options
  • Save androidcodehunter/8bc09e2c7fb2dcd54b533dc5972b698e to your computer and use it in GitHub Desktop.
Save androidcodehunter/8bc09e2c7fb2dcd54b533dc5972b698e to your computer and use it in GitHub Desktop.
<declare-styleable name="IconButton">
<attr name="iconPadding" format="dimension"/>
</declare-styleable>
public class CenterIconButton extends Button {
protected int drawableWidth;
protected DrawablePositions drawablePosition;
protected int iconPadding;
// Cached to prevent allocation during onLayout
Rect bounds;
public enum DrawablePositions {
NONE,
LEFT_AND_RIGHT,
LEFT,
RIGHT
}
public CenterIconButton(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
bounds = new Rect();
applyAttributes(attrs);
}
public CenterIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CenterIconButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
protected void applyAttributes(AttributeSet attrs) {
// Slight contortion to prevent allocating in onLayout
if (null == bounds) {
bounds = new Rect();
}
if (attrs == null) return;
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.IconButton);
int paddingId = typedArray.getDimensionPixelSize(R.styleable.IconButton_iconPadding, 0);
setIconPadding(paddingId);
typedArray.recycle();
}
public void setIconPadding(int padding) {
iconPadding = padding;
requestLayout();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Paint textPaint = getPaint();
String text = getText().toString();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int textWidth = bounds.width();
int factor = (drawablePosition == DrawablePositions.LEFT_AND_RIGHT) ? 2 : 1;
int contentWidth = drawableWidth + iconPadding * factor + textWidth;
int horizontalPadding = (int) ((getWidth() / 2.0) - (contentWidth / 2.0));
switch (drawablePosition) {
case LEFT:
setGravity(Gravity.CENTER_VERTICAL);
setPadding(horizontalPadding, getPaddingTop(), 0, getPaddingBottom());
break;
case RIGHT:
setGravity(Gravity.CENTER_VERTICAL);
setPadding(0, getPaddingTop(), horizontalPadding, getPaddingBottom());
break;
case LEFT_AND_RIGHT:
setGravity(Gravity.CENTER_VERTICAL);
setPadding(horizontalPadding + 24, getPaddingTop(), 24, getPaddingBottom());
break;
default:
setGravity(Gravity.CENTER);
setPadding(0, getPaddingTop(), 0, getPaddingBottom());
}
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
if (left != null && right != null) {
drawableWidth = left.getIntrinsicWidth() + right.getIntrinsicWidth();
drawablePosition = DrawablePositions.LEFT_AND_RIGHT;
} else if (left != null) {
drawableWidth = left.getIntrinsicWidth();
drawablePosition = DrawablePositions.LEFT;
} else if (right != null) {
drawableWidth = right.getIntrinsicWidth();
drawablePosition = DrawablePositions.RIGHT;
} else {
drawablePosition = DrawablePositions.NONE;
}
requestLayout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment