Android. Listview not wrap_content inside ScrollView

I faced problem when I tried to put ListView inside ScrollView. ListView is not wrapping content. It is only showing first item on the list. In this post I will try to explain how to make ListView wrap content.
First of all, create this class –

package com.globalsolutions.medapp.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class ExpandedListView extends ListView
    private android.view.ViewGroup.LayoutParams params;
    private int oldCount = 0;

    public ExpandedListView(Context context, AttributeSet attrs)
        super(context, attrs);

    protected void onDraw(Canvas canvas)
        if (getCount() != oldCount)
            int height = getChildAt(0).getHeight() + 1 ;
            oldCount = getCount();
            params = getLayoutParams();
            params.height = getCount() * height;


In your xml file, write this instead of ListView tag:

               android:divider="@color/list_divider" >

In your activity, initialize your custom ListView:

ExpandedListView mDrawerList = (ExpandedListView) findViewById(;