I like neither of the answers. Simply set the inputType and the TextView will adapt to its content
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
Tested on a Nexus One (2.3) and Nexus 4 (4.4)
I just thought I'd add that if you use :
android:inputType="textMultiLine"
Then the view stops being clickable. I was trying to get multi line textviews in my slide-drawer menu which obviously needs to respond to clicks.
The android:singleLine="false"
worked fine though.
I do not like the solution that forces the number of lines in the text view. I rather suggest you solve it via the solution proposed here. As I see the OP is also struggling with making text view look like proper in table and shrinkColumns
is the correct directive to pass in to achieve what is wanted.
you can use android:inputType="textMultiLine"
<TextView android:id="@+id/address1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"
android:inputType="textMultiLine"
android:text="Johar Mor, Gulistan-e-Johar, Karachi" />
for me non of the solutions did not work. I know it is not a complete answer for this question, but sometimes it can help.
I put 4 textviews in a vertical linearlayout.
<Linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation:"vertical"
...>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line1"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line2"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line3"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line4"
.../>
</LinearLayout>
This solution is good for cases that the text view text is constant, like labels.
First replace "\n"
with its Html
equavalent "<br>"
then call Html.fromHtml()
on the string. Follow below steps:
String text= model.getMessageBody().toString().replace("\n", "<br>")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))
This works perfectly.