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 – ExpandedListView.java:

package com.globalsolutions.medapp.ui;

import android.content.Context;
import android.graphics.Canvas;
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);
    }

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

        super.onDraw(canvas);
    }
}

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

            <com.globalsolutions.medapp.ui.ExpandedListView
               android:id="@+id/lvButtons"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginBottom="@dimen/margin_between_text_and_buttons"
               android:layout_marginTop="@dimen/margin_between_text_and_buttons"
               android:background="@color/app_background"
               android:divider="@color/list_divider" >
            </com.globalsolutions.medapp.ui.ExpandedListView>

In your activity, initialize your custom ListView:

ExpandedListView mDrawerList = (ExpandedListView) findViewById(R.id.lvButtons);