Skip to content Skip to sidebar Skip to footer

Setting Popupwindow To Have A Maximum Height

I inflate a ListView inside a PopupWindow and I want the popup to behave like this: wrap the listview when its height is < x set the popup's height = x when the listiew's heigh

Solution 1:

I have found the solution by myself. For anyone who will ever stumble upon this problem, the answer is to override the method onMeasure() of the ListView like this:

public class MyListView extends ListView {

public MyListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

public MyListView(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    //set your custom height. AT_MOST means it can be as tall as needed,
    //up to the specified size.

    int height = MeasureSpec.makeMeasureSpec(300,MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec,height);               
}
}

Post a Comment for "Setting Popupwindow To Have A Maximum Height"