-1

How do you center TextViews in xml?

Example:

  <TextView
    android:id="@+id/text_3"
    android:text="@string/text_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#ffffff"
    />
epicness42
  • 1,137
  • 11
  • 12
  • possible duplicate of [can we put layout in center of the screen in Android](http://stackoverflow.com/questions/1499258/can-we-put-layout-in-center-of-the-screen-in-android) – FoamyGuy Jul 20 '12 at 02:16
  • If you searched or google it you will easily get the answer, -1 for your effort... – Daud Arfin Jul 20 '12 at 03:35
  • It was designed to be a helpful index for other people to google. I answered it. This is a "q and a" style question. I know the answer and was only trying to help people who don't. – epicness42 Jul 20 '12 at 04:01
  • You want to center TextView in its parent layout or center displayed text in the TextView? – Clover Jul 20 '12 at 02:32
  • Center displayed text in the TextView. – epicness42 Jul 20 '12 at 04:08
  • @epicness_studios The titles make it seem like it is not a dupe, but the subject matter is the same. The ways you center a layout are the same as the ways you center any View including TextView. Between the 4 answers given in that question they cover everything that was posted as an answer here...Note that I see nothing wrong with you posting it, vote to close is not personal. And even with the question "closed" it will remain online for others to find in the future. – FoamyGuy Jul 20 '12 at 13:05
  • possible duplicate of [How do I center text horizontally and vertical in a TextView in Android?](http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android) – Sam Jul 21 '12 at 19:36

3 Answers3

2

set

android:gravity="center"

in your Layout and Textview . Look this Exam.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="@string/hello" />

</LinearLayout>
jiw_cs
  • 761
  • 6
  • 2
1

You must add the layout_gravity to the TextView

try this

<TextView
    android:id="@+id/text_3"
    android:text="@string/text_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#ffffff"
    android:layout_gravity="center"
    />
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
0

You can center text in xml using,

android:gravity="center"

or

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
epicness42
  • 1,137
  • 11
  • 12
  • Setting the gravity to center will center the text in the TextView, whereas centering the TextView vertically and horizontally will do just that. – epicness42 Jul 20 '12 at 18:15