Shishir Gupta
Shishir Gupta

Reputation: 1582

Android - Button height not decreasing to wrap text

I set the button's height via XML. Suppose it to be X

<Button
    android:layout_height="wrap_contnet"
    android:textSize = " <X> dp"
    ....
    ....
/>

If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-

shown here

As shown above, text size is so small but still height doesn't decrease! How can I force it to be of appropriate height?

Thanx in advance!

Upvotes: 1

Views: 4341

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe

EDIT

wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button

EDIT 2

As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview

Upvotes: 5

Yang Peiyong
Yang Peiyong

Reputation: 11606

Just Set android:minHeight="xxdp"

Upvotes: 1

Related Questions