\"wrap_content\" is not working in my button, it currently looks like this:
now want
____________
| | _________
wrap_content will not shrink a button smaller than the background. If you are already using a nine patch for the background, then cut back the size of the regions to the smallest they can be. If there's not a gradient, then you only really need about two pixels to define the stretchable regions and then make sure the central area is as small as it can be. Use the padding (right and bottom lines on ninepatch) to allocate the padding.
If you're using a drawable image (not a nine-patch or xml drawable) then make it a nine patch drawable
If you don't want to solve the problem in the image, you will need to set the layout_height to a fixed value to force the image to shrink (ie don't use wrap_content)
Adding just minHeight doesn't work. You also need to set verticalPadding to 0dp
<Button
android:id="@+id/button1"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="1dp"
android:paddingVertical="0dp"
android:textSize="14sp"
android:text="Login"/>
Changing the background didn't work, as well as padding="@null".
I ended up using a TextView because my button was simple enough for it to suffice:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Join"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:textSize="@dimen/xsmall_text_size"
android:textColor="@drawable/button_text_selector"
android:background="@color/turquoise"
android:textStyle="bold"
android:onClick="buttonOnClick"
android:clickable="true"/>
Button
is a View
and the android:minHeight
attribute is set 48dip
default. You can set minHeight
lower than your actual text 'aaaaa' such as 1dp. Then wrap_content
will work. But I don't think this is recommended.
One solution is to add android:padding="@null"
to the button in your XML. This will override any existing padding being enforced.
There is also an issue I've come across where a button will not shrink smaller than the padding defined in the default button style. I highlighted this in my own question some time ago but haven't had an answer that resolves it yet.
The background is adding unwanted padding