get GridView Height - Android User Interface

Android examples for User Interface:GridView

Description

get GridView Height

Demo Code


//package com.java2s;

import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;

public class Main {
    public static int getGridViewHeight(GridView gridView, int itemHeight) {
        ListAdapter adapter = gridView.getAdapter();

        int rowCount = 0;
        int numColimns = gridView.getNumColumns();
        if (numColimns == -1) {
            numColimns = 2;//from w  ww  .j  a va2s .  c  om
        }

        float numLine = adapter.getCount() / numColimns;
        int numLineInt = adapter.getCount() / numColimns;

        if (numLine - (float) numLineInt < 1.0) {
            rowCount = numLineInt + 1;
        } else if (numLine - (float) numLineInt == 0.0) {
            //  
            rowCount = numLineInt;
        }

        int totalHeight = 0;
        totalHeight = itemHeight * rowCount;

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight
                + (gridView.getVerticalSpacing() * (rowCount - 1));
        return params.height;
    }
}

Related Tutorials