1

I have the following code. There's is some preexisting code and I added new changes below. However, I see that in my button, there's some padding on top and bottom. I'm not sure where this is coming from.

class MyButton(context: Context) : Button(context) {
    fun init() {
        setBackgroundColor(Color.parseColor("#880808")) // for testing
        text = context.getString(R.string.some_text)
        setTextColor(ContextCompat.getColor(context, R.color.some_color))
        this.setTextSize(
                TypedValue.COMPLEX_UNIT_DIP, getSizeInDP()
        )
//My new changes below
        isAllCaps = false
        layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).also {
            it.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
            it.setMargins(getMarginSize(), getMarginSize(), getMarginSize(), getMarginSize())
        }
        minWidth = 0
        minHeight = 0
        setPadding(0, 0, 0, 0)
    }
// Pre-existed code
class SomeActivity() { 
    override fun onCreate(savedInstanceState: Bundle?) {
        .....
        val height = (displayMetrics.heightPixels * 0.15).toInt()


        val container = RelativeLayout(this)
        val button = MyButton(this)
        button.init()
        container.addView(button)


        container.setBackgroundColor(Color.TRANSPARENT)
        val myLayoutParams = createContainerLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                headerHeight - 30)
        myLayoutParams.topMargin = 30
        container.layoutParams = myLayoutParams
        .....
    }
}

The button has some padding on the top and bottom. I'm not sure where this padding is coming from when I set padding to 0.

Image: Button at the bottom left of the device

enjoinslip
  • 41
  • 4
  • 1
    Does this answer your question? [Android: TextView: Remove spacing and padding on top and bottom](https://stackoverflow.com/questions/4768738/android-textview-remove-spacing-and-padding-on-top-and-bottom) – Pawel May 11 '23 at 22:43
  • includeFontPadding = 0 didn't help. From layout inspector I see the following minHeight = 48dp minWidth = 88dp measuredHeight=48dp measuredWidth=91dp I'm not sure why my button has minWidth and minHeight when I specifically set them to 0 – enjoinslip May 11 '23 at 23:37
  • I needed to use minimumHeight rather than minHeight. setting minimumHeight = 0 did the trick – enjoinslip May 12 '23 at 00:01

0 Answers0