Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making text vertically centered in a button [duplicate]

Tags:

android

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="&lt;"
    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" />
like image 826
Maedeh HM Avatar asked Aug 16 '16 20:08

Maedeh HM


People also ask

How do I center text vertically in a button?

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!

How do I align text to the center of a button?

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.

How do I align text vertically to icons?

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.


1 Answers

you need to set padding

    android:padding="5dp"
    android:gravity="center"
    android:textSize="12sp"
like image 189
Anurag Shrivastava Avatar answered Oct 30 '22 08:10

Anurag Shrivastava