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: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.
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.
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".
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"
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.
Had to add both gravity
and layout_gravity
to align both TextView
and its content
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
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!
You can use:
TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));
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"));
I use:
android:gravity="center_horizontal"
to center two line text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With