Skip to content

Instantly share code, notes, and snippets.

@fanjavaid
Last active August 8, 2018 14:56
Show Gist options
  • Save fanjavaid/afeff3bef55e3ee6f17e37c0ce22bcb7 to your computer and use it in GitHub Desktop.
Save fanjavaid/afeff3bef55e3ee6f17e37c0ce22bcb7 to your computer and use it in GitHub Desktop.
Center Icon and Text in Button
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.widget.Button
/**
* Created by Fandi Akhmad on 8/2/18.
*/
class CenteredImageButton(context: Context, attrs: AttributeSet) : Button(context, attrs) {
private var mTextPaint: Paint? = null
private var mIconDrawable: Drawable? = null
private var mIconSize = resources.getDimensionPixelSize(R.dimen.icon_size_24)
private var mButtonText: String? = null
private val bounds = Rect()
init {
//...
mTextPaint = Paint().apply {
isAntiAlias = true
color = ContextCompat.getColor(context, android.R.color.black)
textSize = resources.getDimension(R.dimen.text_size_14)
}
}
fun setIconResource(@DrawableRes resource: Int) {
mIconDrawable = ContextCompat.getDrawable(context, resource)
}
fun setButtonText(text: String) {
mButtonText = text
}
fun setButtonTextColor(@ColorRes color: Int) {
mTextPaint?.color = ContextCompat.getColor(context, color)
}
fun setBoldText() {
mTextPaint?.typeface = Typeface.DEFAULT_BOLD
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
mIconDrawable?.setBounds(getLeftPosition(), getTopPosition(),
mIconSize + getLeftPosition(), mIconSize + getTopPosition())
mIconDrawable?.draw(canvas)
mButtonText?.let {
mTextPaint?.getTextBounds(it, 0, it.length, bounds)
}
canvas?.drawText(mButtonText, (mIconSize + getLeftPosition() + 16).toFloat(),
((height / 2) - bounds.centerY() + 5).toFloat(), mTextPaint)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val paddingLeft = resources.getDimensionPixelSize(R.dimen.margin_padding_48)
val paddingRight = resources.getDimensionPixelSize(R.dimen.margin_padding_32)
val desiredWidth = suggestedMinimumWidth + paddingLeft + paddingRight
}
private fun getTopPosition(): Int {
val halfHeightOfDrawable = mIconSize.div(2)
val halfHeightOfView = height / 2
return halfHeightOfView - halfHeightOfDrawable
}
private fun getLeftPosition(): Int {
val halfWidthOfDrawable = (getCompoundPosition() / 2).toInt()
val halfWidthOfView = width / 2
return halfWidthOfView - halfWidthOfDrawable
}
private fun getCompoundPosition(): Float {
val textWidth = mTextPaint?.measureText(mButtonText) ?: 0f
val gapWidth = resources.getDimensionPixelSize(R.dimen.margin_padding_16)
return mIconSize + gapWidth + textWidth
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment