How to change the size of an item in a ListView dynamically?

180 views Asked by At

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>
5

There are 5 answers

0
Neal Ahluvalia On BEST ANSWER

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>

0
Suraj On

Make your list view to be horizontally scrollable

i.e.

Add your

 <ListView> inside <HorizontalScrollView>

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

0
Suraj On

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...

0
Santosh Kathait On

Use for Textview android:singleLine="false"

0
Anand Savjani On

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);