Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap text in textview in Android

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.

I have searched and tried the following:

android:scrollHorizontally="false", android:inputType="textMultiLine", android:singleLine="false" 

But none work..

Can anyone suggest how can I do it.

like image 306
Sameer Avatar asked Jan 31 '11 13:01

Sameer


People also ask

How to enable wrap text in android studio?

“how to wrap code in android studio” Code Answer "File->Settings->Editor->General" tab and check "Use soft wraps in editor".

What is Ellipsize?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.


2 Answers

Constraint Layout

<TextView android:id="@+id/some_textview" android:layout_width="0dp" android:layout_height="wrap_content"  app:layout_constraintLeft_toLeftOf="@id/textview_above" app:layout_constraintRight_toLeftOf="@id/button_to_right"/> 
  • Ensure your layout width is zero
  • left / right constraints are defined
  • layout height of wrap_content allows expansion up/down.
  • Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
  • Ellipses are prob. a good idea with max lines android:ellipsize="end"

0dp width allows left/right constraints to determine how wide your widget is.

Setting left/right constraints sets the actual width of your widget, within which your text will wrap.

Constraint Layout docs

like image 120
Baker Avatar answered Oct 09 '22 07:10

Baker


For me this issue only occurred on Android < 4.0

The combination of parameters I used were:

android:layout_weight="1" android:ellipsize="none" android:maxLines="100" android:scrollHorizontally="false" 

The maxLines count seemed to be the random final piece that made my TextView wrap.

like image 21
Guykun Avatar answered Oct 09 '22 06:10

Guykun