How to wrap text in textview in Android
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
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.
For the case where the TextView
is inside a TableLayout
, the solution is to set android:shrinkColumns="1"
on the TableLayout
. (Replace 1
with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView
.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="@+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.