set Up GridView - Android User Interface

Android examples for User Interface:GridView

Description

set Up GridView

Demo Code


//package com.java2s;
import android.app.Activity;

import android.content.res.Resources;

import android.util.DisplayMetrics;

import android.view.ViewGroup.LayoutParams;

import android.widget.GridView;
import android.widget.LinearLayout;

public class Main {

    public static void setUpGrid(Resources resources, int dimenId,
            int lineSize, int listSize, LinearLayout gridLinear,
            GridView gridView) {/*  w  ww. jav a2  s.  c o  m*/
        LayoutParams lp = (LayoutParams) gridLinear.getLayoutParams();
        lp.width = resources.getDimensionPixelSize(dimenId) * listSize
                / lineSize;
        gridLinear.setLayoutParams(lp);
        gridView.setNumColumns(listSize % lineSize == 0 ? listSize
                / lineSize : listSize / lineSize + 1);
    }

    public static void setUpGrid(Activity activity, int columsNum,
            LinearLayout gridLinear, GridView gridView) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        LayoutParams lp = gridLinear.getLayoutParams();
        lp.width = dm.widthPixels;
        // Display display = activity.getWindowManager().getDefaultDisplay();
        // lp.width = display.getWidth();
        gridLinear.setLayoutParams(lp);
        gridView.setNumColumns(columsNum);
    }
}

Related Tutorials