Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 314
shiami Avatar asked Jan 25 '11 02:01

shiami


People also ask

How can I center my text in android?

android:gravity="center_horizontal" for align text Center horizontally. android:gravity="center_vertical" for align text Center vertically. android:gravity="center" for align text Center both vertically and horizontally.

How do you add multiple lines in TextView?

When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines. When you set the layout_width and layout_height as wrap_content or match_parent , then the TextView widget will use all the available space to display the text you specified as its content.

How do you center TextView in linear layout?

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".

What is multiline text in android Studio?

This tag makes the EditText be at most x many lines tall as specified as value. It accepts an integer value. Note : For multiline EditText by default the cursor and hint text is displayed in the center, you can use android:gravity attribute to set it at top and left of the EditText view : android:gravity="top|left"


6 Answers

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.

like image 103
Kevin Coppock Avatar answered Oct 09 '22 06:10

Kevin Coppock


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

android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
like image 35
kishor.j Avatar answered Oct 09 '22 06:10

kishor.j


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!

like image 30
Shivam Avatar answered Oct 09 '22 04:10

Shivam


You can use:

TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));
like image 40
Dleelk4android Avatar answered Oct 09 '22 06:10

Dleelk4android


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"));
like image 34
Cristian Avatar answered Oct 09 '22 05:10

Cristian


I use:

android:gravity="center_horizontal"

to center two line text

like image 34
Dea Venditama Avatar answered Oct 09 '22 05:10

Dea Venditama