170

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.

Here's my code :

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_centerHorizontal="true"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

I put paddingTop and Bottom because I need some space. PS: My code is bigger; it's in a RelativeLayout.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Tsunaze
  • 3,204
  • 7
  • 44
  • 81

11 Answers11

433

Set also android:gravity parameter in TextView to center.

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.

peter.bartos
  • 11,855
  • 3
  • 51
  • 62
33

use this way

txt.setGravity(Gravity.CENTER);
zzlalani
  • 22,960
  • 16
  • 44
  • 73
bhargavkumar040
  • 800
  • 7
  • 13
25

use this way in xml

   <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time is precious, so love now."
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff"
        />
sakshi
  • 251
  • 3
  • 3
9

You can use the following:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

 <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Your text"
        android:typeface="serif" />
</LinearLayout>

The layout needs to be relative for the "center" property to be used.

gsb
  • 5,520
  • 8
  • 49
  • 76
5

Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!

Also, you should avoid using dp for font size. Use sp instead.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Divya
  • 543
  • 2
  • 13
  • 20
5

Just add these 2 lines;

android:gravity="center_horizontal"
android:textAlignment="center"
Ramandeep Singh
  • 552
  • 6
  • 11
3

add layout_gravity and gravity with center value on TextView

<TextView
    android:text="welcome text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    />
Imam Mubin
  • 294
  • 2
  • 10
1
android:layout_gravity="center"

or

android:gravity="center"

both works for me.

Dinith
  • 839
  • 14
  • 22
1

Or check this out this will help align all the elements at once.

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>
Milan Patel
  • 104
  • 8
0

If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:

chatMsg.setGravity(Gravity.RIGHT);

chatMsg is my textView. I wanted the text to be right aligned and this code worked well.

0

you can easily done this by adding these lines

android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"