How to center a two-line text in a TextView on Android?

AndroidTextTextviewMultilineText Alignment

Android Problem Overview


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!

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"

Solution 3 - Android

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!

Solution 4 - Android

You can use:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));

Solution 5 - Android

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"));

Solution 6 - Android

I use:

android:gravity="center_horizontal"

to center two line text

Solution 7 - Android

Add property

android:lines="2"

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshiamiView Question on Stackoverflow
Solution 1 - AndroidKevin CoppockView Answer on Stackoverflow
Solution 2 - Androidkishor.jView Answer on Stackoverflow
Solution 3 - AndroidShivamView Answer on Stackoverflow
Solution 4 - AndroidDleelk4androidView Answer on Stackoverflow
Solution 5 - AndroidCristianView Answer on Stackoverflow
Solution 6 - AndroidDea VenditamaView Answer on Stackoverflow
Solution 7 - AndroidSiddharthaView Answer on Stackoverflow