Align the text of a button to the bottom

3.8k Views Asked by At

I am facing a problem.
I want to align the text of a button to the bottom with a padding of 20dp. However, the text sticks at the bottom of my button.

<Button
    android:id="@+id/CatButton"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:paddingBottom="20dp"
    android:background="@drawable/rounded_1_selected"
    android:gravity="bottom|center_horizontal"
    android:text="test"/>

Can anyone please help me?

4

There are 4 best solutions below

1
On BEST ANSWER

If you change layout_gravity = "bottom", your button align bottom; but if you change gravity = "bottom", your button's text align bottom. You should do that:

<Button
            android:id="@+id/catButton"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center" 
            android:text="test"
            android:gravity="center|bottom"/>
1
On

Because you set android:gravity="bottom|center_horizontal".

If you want its text shows in center, you should set android:gravity="center".

--Edit--

It was a mistake. I misunderstood.

You should set android:gravity="center|bottom".

0
On

try this

<Button
        android:id="@+id/CatButton"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:paddingBottom="20dp"
        android:gravity="center_vertical|center_horizontal"
        android:text="test"/>
0
On

We can implement it with the help of gravity attribute.

android:gravity="start|bottom"

Eg:

<Button
    android:id="@+id/btn_submit"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_gravity="center" 
    android:text="submit"
    android:gravity="start|bottom"/>