Align the text of a button to the bottom

3.7k 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 answers

1
propoLis 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
Ellie Zou 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
missing17 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
Codemaker2015 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"/>