0

I have a ListView with my own XML. It contains a TextView. There can be 1 up to 6 lines of text.

The problem is that it shows only 2 lines of text. How can I change the size of an item in a ListView dynamically to show the whole text?

My xml, where txt2 contains 1-6 line of text:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Test"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:textColor="#000000"
            />
        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:textColor="#000000"
            />
    </LinearLayout>
</LinearLayout>
moffeltje
  • 4,095
  • 4
  • 24
  • 49
user1468102
  • 381
  • 5
  • 14

5 Answers5

1

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 >
<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="Test"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:textColor="#000000"
        />
    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:textColor="#000000"
        />
</LinearLayout>

Neal Ahluvalia
  • 1,528
  • 1
  • 9
  • 20
0

Use for Textview android:singleLine="false"

Santosh Kathait
  • 1,434
  • 1
  • 9
  • 21
0

Use this to calculate dynamically change

String abc ="text which you will use";
TextView textView = null; // assign here
textView.setMaxLines(abc.length()/ 40);
textView.setMinLines(abc.length()/ 40);
Anand Savjani
  • 2,334
  • 2
  • 23
  • 38
0

Make your list view to be horizontally scrollable

i.e.

Add your

 <ListView> inside <HorizontalScrollView>

Now you can horizontally scroll to see full text...

Suraj
  • 607
  • 5
  • 17
0

You can also Just give a fixed

layout_width for TextView.

And the whole 1-6 lines of text will wrap to according to height...

Suraj
  • 607
  • 5
  • 17