Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center text android button

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.

like image 819
dieters Avatar asked Jun 11 '12 10:06

dieters


People also ask

How do you center text on Android?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do I center a button on Android?

If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.

How do I center font in 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.


1 Answers

It's easy peasy, you can put this:

android:textAlignment="center" 
like image 141
Cabezas Avatar answered Nov 09 '22 22:11

Cabezas