brian
brian

Reputation: 6912

How to get height of AlertDialog on Android

I use below code to show a dialog:

private AlertDialog MyDialog;
public void adddialog() {
    AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
    MyBuilder.setTitle("Title")
    MyBuilder .setView(ListView);
    MyDialog = MyBuilder.create();
}

The ListView may un-fix rows.
But I want to get the MyDialog's height.
How can I do to get it?

Upvotes: 2

Views: 3793

Answers (3)

buptcoder
buptcoder

Reputation: 2702

I checked the height, it means Information about how tall the view wants to be.. SO maybe you can try like this: You can get the height with:

     int height = MyDialog.getWindow().getDecorView().getHeight();

Also, you can set the height With:

     MyDialog.getWindow().setLayout(width, height);

Notice: all method should be called after MyDialog.show();

Upvotes: 1

Mani
Mani

Reputation: 777

In order to get the ListView visible height, you need to get height of one row of the list and multiply by the number of elements ( which are presently visible).

Something like this.

        Adapter ListAdapter = listview.getAdapter();
        View mView  = ListAdapter.getView(0, null, listview);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int requiredHeight = listview.getChildCount() * mView.getMeasuredHeight();

Upvotes: 1

Arun C
Arun C

Reputation: 9035

You can use getHeight() of the listView Object instead of getting height of dialog.

Upvotes: 0

Related Questions