Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do You Center a TextView in Layout?

I have a complex layout, part of which features a value centered over a label, with + and - buttons on either side of the value. I want the value to center between the buttons, whether it is "1" or "99". It looks fine when it's a 2-digit number like "99", but when it's a single digit the number is left-justified. How do I properly center that value?

Here's the portion of my layout that does this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_above="@id/runway_label"     android:layout_centerHorizontal="true"     android:orientation="horizontal">      <ImageView         android:id="@+id/dec_runway_button"         android:src="@drawable/minus_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_vertical"/>      <TextView         android:id="@+id/runway_value"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textColor="#FFFFFF"         android:textStyle="bold"         android:textSize="40.0sp"         android:minWidth="50sp"         android:layout_centerInParent="true"         android:layout_gravity="center"         android:shadowColor="#333333"         android:shadowDx="2.0"         android:shadowDy="2.0"         android:shadowRadius="3.0" />      <ImageView         android:id="@+id/inc_runway_button"         android:src="@drawable/plus_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_vertical"/>  </LinearLayout> 
like image 665
Ken Avatar asked May 06 '10 19:05

Ken


People also ask

How do I center a TextView layout?

Instead, to center the TextView set android:gravity="center" for the RelativeLayout itself. Because the android:gravity attribute does not start with layout_ it refers to the RelativeLayout contents, in this case the TextView.

How do I center text in a text box in android Studio?

To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.

How do you align text in LinearLayout?

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".

How can I center text programmatically in android?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.


1 Answers

It sounds like you want to center the text within the TextView, not the TextView in the Layout.

You're already explicitly setting the minWidth of the TextView, which is good. Have you tried using android:gravity="center_horizontal" for the TextView? NOT layout_gravity which is the gravity of the TextView within the parent, in this case LinearLayout.

like image 90
Brandon O'Rourke Avatar answered Sep 22 '22 02:09

Brandon O'Rourke