88

I want to let it looks like this:

|    two    |
|   lines   |

Here is the current layout, not working at all.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two\nlines"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true"/>
</RelativeLayout>

Any idea? Thanks!

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
shiami
  • 7,174
  • 16
  • 53
  • 68

7 Answers7

229

If you just want to center it (I'm assuming the \n is working to split the lines), just add android:gravity="center_horizontal" rather than layout_gravity.
Using layout gravity moves the actual TextView, using gravity affects the content of the TextView.

Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
21

Had to add both gravity and layout_gravity to align both TextView and its content

android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
kishor.j
  • 223
  • 3
  • 7
4

if your width is wrap_content, you have to set gravity and layout_gravity both as "center_horizontal"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="two\nlines"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"/>

However, if your width is "match_parent, you will only have to set gravity as "center_horizontal"

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="two\nlines"
    android:gravity="center_horizontal"/>

Hope it helps!

Shivam
  • 1,086
  • 7
  • 8
2

You can use:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));
Tisho
  • 8,320
  • 6
  • 44
  • 52
2

I use:

android:gravity="center_horizontal"

to center two line text

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
1

I think you must do it from Java:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/the_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true"/>
</RelativeLayout>

Then:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two<br/>lines"));
Cristian
  • 198,401
  • 62
  • 356
  • 264
-3

Add property

android:lines="2"

where 2 is no of lines you can set this no as your requirment

Kampai
  • 22,848
  • 21
  • 95
  • 95
Siddhartha
  • 29
  • 1
  • 1
    this does not set the TextView centered, it only forces it to have two lines. Still, this answer is not so bad it needs to be downvoted. – Shishdem Mar 19 '15 at 14:14
  • It won't make any impact as requested. Alignment will not be centred. – deva11 Dec 30 '17 at 06:18