get GridView Column Width - Android User Interface

Android examples for User Interface:GridView

Description

get GridView Column Width

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;
import android.os.Build;
import android.widget.GridView;
import java.lang.reflect.Field;

public class Main {
    @SuppressLint("NewApi")
    public static int getColumnWidth(final GridView gridView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            return gridView.getColumnWidth();
        else {// w  ww  . j  ava2 s .  c  o m
            try {
                Field field = GridView.class
                        .getDeclaredField("mColumnWidth");
                field.setAccessible(true);
                Integer value = (Integer) field.get(gridView);
                field.setAccessible(false);
                return value;
            } catch (NoSuchFieldException e) {

            } catch (IllegalAccessException e) {

            }
        }
        return 200;
    }
}

Related Tutorials