1

To increase the listview height, I am using exbandable ListView as,

public class ExpandableListView extends ListView {

    private android.view.ViewGroup.LayoutParams params;
    private int old_count = 0;

    public ExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getCount() != old_count) {
            old_count = getCount();
            params = getLayoutParams();
            params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
            System.out.println("params h "+params.height+" "+params);
            setLayoutParams(params);
        }

        super.onDraw(canvas);
    }

}

layout.xml as,

<com.jems.realtimedata.ExpandableListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"      
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="25dp"
                android:background="@color/list_back"
                android:cacheColorHint="#00000000"
                android:divider="@drawable/line"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:paddingTop="10dp"
                android:scrollbars="none" />

But,last item of list not displaying properly. I used utility class also,but no change in list.Any other solution to extend the view.Please help me to solve this issues.

user970607
  • 73
  • 1
  • 10
  • Does this answer your question? [How to change ListView height dynamically in Android?](https://stackoverflow.com/questions/6071131/how-to-change-listview-height-dynamically-in-android) – Amir Dora. Oct 05 '20 at 10:57

2 Answers2

2

change line

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);

to this:

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 1 : 0);

this only works for a list with single-line items.

Teddy
  • 92
  • 7
1

This question is already posted.So have a look at this, and you can customize it according to your need in Expandable list

This link will help you:

How to change ListView height dynamically in Android?

Community
  • 1
  • 1
Agnihotri.
  • 426
  • 3
  • 12