Center text android button
Asked Answered
M

7

29

My question is very simple. How do I center the text on a button in android? I tried to set padding to 0, gravity to center but the result when i run it still that the text is horizontal centred but not vertical. The text is a bit shifted to the bottom.

<Button
    android:id="@+id/btnSame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:background="@drawable/layout_button_different"
    android:gravity="center_vertical|center_horizontal"
    android:height="30dp"
    android:padding="0dip"
    android:text="@string/equals"
    android:textColor="@drawable/layout_button_different"
    android:textSize="50dp"
    android:width="70dp" />

My be also relevant: In activity I do this:

btnEquals.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.up2), null, null);
btnEquals.setPadding(0, 5, 0, 0);
btnEquals.setTextSize(15);

This works but after this I set this:

btnEquals.setCompoundDrawables(null, null, null, null);
btnEquals.setPadding(0, 0, 0, 0);

Result is a bad vertical alignment.

Monarchal answered 11/6, 2012 at 10:38 Comment(0)
B
23

Your existing layout cannot hope to center the text due to the sizes you have selected. You've set the button height to 30dp and your textSize is 50dp, and for whatever reason, Android is unable to deal with that and center the text. If you make your button larger or your text smaller you'll see that the centering works.

Buckeen answered 11/6, 2012 at 10:50 Comment(6)
if Height is 30dp then what is the use of android:layout_height="wrap_content"?Ribosome
@hotveryspicy -- put the button layout into an xml and load it up, and you'll see that the wrap_content appears to not care about the spill of the text in this case. I don't know why that happens, but it's what happens (at least when viewed through the Eclipse visual display of the layout; I didn't put it on a device).Buckeen
Thanks but deleting android:layout_height or android:layout_width resolves in a RuntimeException.Monarchal
@Monarchal -- no one has suggested you delete either of those; they are required by Android. android:textSize="50dp" is the issue -- especially if you're changing it to 15 in code; either get rid of it or just set it to 15 in the layout.Buckeen
ahah i had the exact problem :) you saved me weekend :)Procrustean
android:gravity vs android:layout_gravity android:gravity is used to specify the gravity of the content of the view. android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.Ansley
O
51

It's easy peasy, you can put this:

android:textAlignment="center"
Oppress answered 19/6, 2014 at 10:19 Comment(4)
This is a good answer, but I think you can simplify it into a single line android:textAlignment="center".Footman
Thanks @Alex for your answer.Oppress
Not work .......Indented
@reza_khalafi, Are you sure?Oppress
B
23

Your existing layout cannot hope to center the text due to the sizes you have selected. You've set the button height to 30dp and your textSize is 50dp, and for whatever reason, Android is unable to deal with that and center the text. If you make your button larger or your text smaller you'll see that the centering works.

Buckeen answered 11/6, 2012 at 10:50 Comment(6)
if Height is 30dp then what is the use of android:layout_height="wrap_content"?Ribosome
@hotveryspicy -- put the button layout into an xml and load it up, and you'll see that the wrap_content appears to not care about the spill of the text in this case. I don't know why that happens, but it's what happens (at least when viewed through the Eclipse visual display of the layout; I didn't put it on a device).Buckeen
Thanks but deleting android:layout_height or android:layout_width resolves in a RuntimeException.Monarchal
@Monarchal -- no one has suggested you delete either of those; they are required by Android. android:textSize="50dp" is the issue -- especially if you're changing it to 15 in code; either get rid of it or just set it to 15 in the layout.Buckeen
ahah i had the exact problem :) you saved me weekend :)Procrustean
android:gravity vs android:layout_gravity android:gravity is used to specify the gravity of the content of the view. android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.Ansley
P
1

There are three systems to consider. The xml layout designer, the simulator, and real phone. The text in the button if you specify gravity="center" will look like this. In xml layout designer it will look like the leftmost letter is in the center of the button but the rest of the letters are to the right. (looks incorrect) In simulator, the text shows well in the center. (correct) In phone, the text shows well in the center. (correct)

Prognathous answered 27/12, 2013 at 2:54 Comment(1)
Thanks for this answer, I spent 15 minutes wondering what I was doing wrong and it turns out it just 'looks' incorrectWold
A
1

You can just add this line to your Button in layout xml code:

android:gravity="center"
Anisette answered 28/4, 2020 at 3:2 Comment(0)
B
0

You can also use negative numbers in setPadding. This may help if your font is large and your button is small. Try also setting the top or bottom to a more negative number if it's still off center.

Barrier answered 20/12, 2013 at 23:8 Comment(0)
C
0

You just have to set layout_height to wrap_content and vary the padding attribute for whatever value you want

Carillo answered 28/1, 2021 at 15:3 Comment(0)
K
-2
<Button
                android:id="@+id/AnswerE"
                style="@style/defaultbuttonstyle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:onClick="AnswerCheck"
                android:text=" E "
                android:textStyle="bold" />

This worked for me

Kolkhoz answered 8/10, 2012 at 10:11 Comment(1)
you did not made it clear what attribute will help centering the content of the button, in the provided question content is "text" and the attribute android:gravity="center" help you center the content.Ansley

© 2022 - 2024 — McMap. All rights reserved.