Check if device screen size is small. - Android User Interface

Android examples for User Interface:Screen Size

Description

Check if device screen size is small.

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.Configuration;

public class Main {
    public static final String SCR_SIZE_SMALL = "small",
            SCR_SIZE_NORMAL = "normal", SCR_SIZE_LARGE = "large",
            SCR_SIZE_XLARGE = "xlarge";

    /**/*from w w  w  .  j  ava2 s .  co  m*/
     * Check if device screen size is small.
     * @param context The activity context or application context.
     * @return true if screen size is small, false if not.
     */
    public static boolean isScrSizeSmall(Context context) {
        return getSizeName(context).equals("small");
    }

    /**
     * Get the device size name.
     * @return String value of device size name(small, normal, large, xlarge).
     */
    public static String getSizeName(Context context) {
        int screenLayout = context.getResources().getConfiguration().screenLayout;
        screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;

        switch (screenLayout) {
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            return SCR_SIZE_SMALL;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            return SCR_SIZE_NORMAL;
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            return SCR_SIZE_LARGE;
        case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
            return SCR_SIZE_XLARGE;
        default:
            return "undefined";
        }
    }
}

Related Tutorials