I have buttons in RelativeLayout
. Height and width are not wrap_content
. I noticed that the text in Button
is not vertically centered. How can I fix this problem in my code?
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="<"
android:id="@+id/buttonPreviousDay"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/editTextShowDate"
android:layout_marginEnd="10dp"
android:textSize="20dp"
android:background="@drawable/button_shape"
android:textStyle="bold" />
Use line-height to center it vertically. I usually use the same value as its height. Works only when you know the height. I set height to 0 and line-height to zero and this worked!
We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.
Use line-height for vertical centering with a fixed height To vertically center a single line of text or an icon within its container, we can use the line-height property. This property controls the height of a line in a run of text and adds an equal amount of space above and below the line-box of inline elements.
you need to set padding
android:padding="5dp"
android:gravity="center"
android:textSize="12sp"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With