set GridView Height By My Self - Android User Interface

Android examples for User Interface:GridView

Description

set GridView Height By My Self

Demo Code


//package com.java2s;

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

public class Main {

    public static void setGridViewHeightByMySelf(GridView gridView,
            int itemHeight) {
        ListAdapter adapter = gridView.getAdapter();

        if (adapter == null) {
            return;
        }/*from   ww w . j a va 2  s  . c o  m*/
        int rowCount = 0;
        int numColimns = gridView.getNumColumns();
        if (numColimns == -1) {
            numColimns = 2;
        }

        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));
        gridView.setLayoutParams(params);

    }
}

Related Tutorials