Java tutorial
//package com.java2s; //License from project: Apache License import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration; import android.os.Build; public class Main { public static final boolean API_13 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2; @TargetApi(13) public static int getColumnCount(Context context) { if (!API_13) { int screenSize = context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; if (screenSize == Configuration.SCREENLAYOUT_SIZE_SMALL) return 1; int orientation = context.getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) return 2; } else { int screenWidthDp = context.getResources().getConfiguration().screenWidthDp; // if (screenWidthDp > 820) return 4; if (screenWidthDp >= 720) return 3; if (screenWidthDp >= 480) return 2; } return 1; } }