Danny Archer
Danny Archer

Reputation: 215

Android TextView doesn't do linebreak in ListView (multiline text)

I have created a 3-level ExpandableListView and have the problem that the TextViews which are used for the 2nd and 3rd level do not support line-breaks if the content is too long. It should be dynamically over more than one line, if needed. The 1st level TextView does it well (automatically) and I actually had the same settings in the xml for all three TextViews. Followed are the layout xmls, the one TextView with the id groupname is for the 2nd level (e.g. the first red X in the picture below) and the one with id childname is for the 3rd level (e.g. the second and third red X in the picture below). It should all be like at the green hook in the picture.

"singleLine=false" seems not to work. Also tried some different options found in other SO posts, but what I've testet haven't worked for me. Like ellipsize, scroll horizontale, different layout_width and so on. The only thing worked is to set a fixed layout_width on x hundred dp, but this is not dynamically, I'm right?

Would be great if anybody could help me with this. Lot of thanks!

Here's a screenshot:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/childname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:layout_marginLeft="60dp"
    android:textColor="#AAAAAA"
    android:singleLine="false"
    android:text=""
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/groupname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="60dp"
    android:textColor="#555555"
    android:singleLine="false"
    android:gravity="center_vertical"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium" />

Upvotes: 2

Views: 6661

Answers (3)

RRTW
RRTW

Reputation: 3190

I had add this attribute to my TextView inside ListView, and makes it do line break correct.

android:maxWidth="xxxdp"

F.Y.R.

Upvotes: 1

Amalan Dhananjayan
Amalan Dhananjayan

Reputation: 2287

Add this line in your xml

android:inputType="textMultiLine"

or

Add text using coding like this, where you can add line break using '\n'(But here you have to manually add breaks where you want them)

TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");

Results will be

Hi
Hello
How are You

Edit Accepted Answer - removing the line 'android:layout_alignParentLeft="true"

Upvotes: 6

Naveen
Naveen

Reputation: 1703

try using LinearLayout instead of RelativeLayout as parent for the TextView

Upvotes: 1

Related Questions