is Tablet if width is greater than 960 pixel - Android Graphics

Android examples for Graphics:Pixel

Description

is Tablet if width is greater than 960 pixel

Demo Code


//package com.java2s;

import android.content.Context;

import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;

public class Main {
    private static final String TAG = "ViewUtils";

    private static final int THRESHOLD_TABLET_WIDTH_DP = 960;

    public static boolean isTablet(Context context) {
        if (context == null) {
            Log.w(TAG, "Argument 'context' is null at isTablet(Context)");
            return false;
        }/*from  ww w .  jav  a 2s  . c  o m*/

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int thresholdWidth = dipToPixelSize(THRESHOLD_TABLET_WIDTH_DP,
                context);
        int deviceWidth = Math.max(metrics.widthPixels,
                metrics.heightPixels);
        if (deviceWidth >= thresholdWidth) {
            return true;
        }
        return false;
    }

    public static int dipToPixelSize(float dip, Context context) {
        return dimensionToPixelSize(TypedValue.COMPLEX_UNIT_DIP, dip,
                context);
    }

    public static int dimensionToPixelSize(int unit, float dimension,
            Context context) {
        if (context == null) {
            Log.w(TAG,
                    "Argument 'context' is null at dimensionToPixelOffset(int, int, Context)");
            return 0;
        }
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();

        float strictValue = dimensionToPixelStrictly(unit, dimension,
                metrics);
        final int res = (int) (strictValue + 0.5f);
        if (res != 0)
            return res;
        if (dimension == 0)
            return 0;
        if (dimension > 0)
            return 1;
        return -1;
    }

    private static float dimensionToPixelStrictly(int unit, float value,
            DisplayMetrics metrics) {
        switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
        }
        return 0;
    }
}

Related Tutorials