Example usage for android.util DisplayMetrics DENSITY_MEDIUM

List of usage examples for android.util DisplayMetrics DENSITY_MEDIUM

Introduction

In this page you can find the example usage for android.util DisplayMetrics DENSITY_MEDIUM.

Prototype

int DENSITY_MEDIUM

To view the source code for android.util DisplayMetrics DENSITY_MEDIUM.

Click Source Link

Document

Standard quantized DPI for medium-density screens.

Usage

From source file:Main.java

/**
 * The screen density expressed as dots-per-inch.
 * @param density_dpi "densityDpi"/*from w ww.  j a va  2  s  . com*/
 */
public static String getDensityDPIStr(int density_dpi) {
    switch (density_dpi) {
    case DisplayMetrics.DENSITY_LOW://120
        return "DENSITY_LOW";
    case DisplayMetrics.DENSITY_MEDIUM://160
        return "DENSITY_MEDIUM/DENSITY_DEFAULT";
    //case DisplayMetrics.DENSITY_DEFAULT://160
    //    return "DENSITY_DEFAULT";
    case DisplayMetrics.DENSITY_TV://213
        return "DENSITY_TV";
    case DisplayMetrics.DENSITY_HIGH://240
        return "DENSITY_HIGH";
    case DisplayMetrics.DENSITY_280://280
        return "DENSITY_280";
    case DisplayMetrics.DENSITY_XHIGH://320
        return "DENSITY_XHIGH";
    case DisplayMetrics.DENSITY_360://360
        return "DENSITY_360";
    case DisplayMetrics.DENSITY_400://400
        return "DENSITY_400";
    case DisplayMetrics.DENSITY_420://420
        return "DENSITY_420";
    case DisplayMetrics.DENSITY_XXHIGH://480
        return "DENSITY_XXHDPI";
    case DisplayMetrics.DENSITY_560://560
        return "DENSITY_560";
    case DisplayMetrics.DENSITY_XXXHIGH://640
        return "DENSITY_XXXHDPI";
    default:
        return UNKNOWN;
    }
}

From source file:Main.java

/**
 * Gets device's display density.//from  ww w  .ja va  2  s . c  o m
 * @since   0.1.0
 * @param   aContext The context from which the display metrics information is retrieved.
 * @return   The density in String or an empty String if display metrics is not retrievable.
 */
public static String getScreenDpi(Context aContext) {
    DisplayMetrics metrics = aContext.getResources().getDisplayMetrics();
    switch (metrics.densityDpi) {
    case DisplayMetrics.DENSITY_LOW:
        return "ldpi";
    case DisplayMetrics.DENSITY_MEDIUM:
        return "mdpi";
    case DisplayMetrics.DENSITY_HIGH:
        return "hdpi";
    case DisplayMetrics.DENSITY_XHIGH:
        return "xhdpi";
    case DisplayMetrics.DENSITY_XXHIGH:
        return "xxhdpi";
    case DisplayMetrics.DENSITY_XXXHIGH:
        return "xxxhdpi";
    default:
        return "";
    }
}

From source file:Main.java

public static Drawable getIconFromPackageName(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        try {/*from w w  w  . j  a  v  a 2  s . c om*/
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

            int displayMetrics[] = { DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_XXHIGH,
                    DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM,
                    DisplayMetrics.DENSITY_TV };

            for (int displayMetric : displayMetrics) {
                try {
                    Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,
                            displayMetric);
                    if (d != null) {

                        return d;
                    }
                } catch (Resources.NotFoundException e) {
                }
            }

        } catch (Exception e) {
            // Handle Error here
        }
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    return appInfo.loadIcon(pm);
}

From source file:Main.java

public static String getDensity(Context ctx) {
    String densityStr = null;//from  w ww . j  av a2 s .  c  om
    final int density = ctx.getResources().getDisplayMetrics().densityDpi;
    switch (density) {
    case DisplayMetrics.DENSITY_LOW:
        densityStr = "LDPI";
        break;
    case DisplayMetrics.DENSITY_MEDIUM:
        densityStr = "MDPI";
        break;
    case DisplayMetrics.DENSITY_TV:
        densityStr = "TVDPI";
        break;
    case DisplayMetrics.DENSITY_HIGH:
        densityStr = "HDPI";
        break;
    case DisplayMetrics.DENSITY_XHIGH:
        densityStr = "XHDPI";
        break;
    case DisplayMetrics.DENSITY_400:
        densityStr = "XMHDPI";
        break;
    case DisplayMetrics.DENSITY_XXHIGH:
        densityStr = "XXHDPI";
        break;
    case DisplayMetrics.DENSITY_XXXHIGH:
        densityStr = "XXXHDPI";
        break;
    }
    return densityStr;
}

From source file:Main.java

public static int getImageButtonSize(Activity activity) {
    if (mImageButtonSize == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mImageButtonSize = 16;// w  w  w  . j  av  a  2s .  c o  m
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mImageButtonSize = 32;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mImageButtonSize = 48;
            break;
        default:
            mImageButtonSize = 32;
        }
    }

    return mImageButtonSize;
}

From source file:Main.java

/**
 * Get the required size of the favicon, depending on current screen density.
 * @param activity The current activity.
 * @return The size of the favicon, in pixels.
 *///from   ww w  .j a  v a 2s. c o  m
public static int getFaviconSize(Activity activity) {
    if (mFaviconSize == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mFaviconSize = 12;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mFaviconSize = 24;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mFaviconSize = 32;
            break;
        default:
            mFaviconSize = 24;
        }
    }

    return mFaviconSize;
}

From source file:Main.java

/**
 * Get the required size of the favicon, depending on current screen density.
 * @param activity The current activity.
 * @return The size of the favicon, in pixels.
 *//*from   ww w  . j av a  2s.  c o  m*/
public static int getFaviconSizeForBookmarks(Activity activity) {
    if (mFaviconSizeForBookmarks == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mFaviconSizeForBookmarks = 12;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mFaviconSizeForBookmarks = 16;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mFaviconSizeForBookmarks = 24;
            break;
        default:
            mFaviconSizeForBookmarks = 16;
        }
    }

    return mFaviconSizeForBookmarks;
}

From source file:Main.java

/**
 * This method converts device specific pixels to density independent pixels.
 *
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 *//*w  w w. ja va  2s  .c  o m*/
public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return px / (metrics.densityDpi / (float) DisplayMetrics.DENSITY_MEDIUM);
}

From source file:Main.java

/**
 * This method converts dp unit to equivalent pixels, depending on device density.
 *
 * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent px equivalent to dp depending on device density
 *//*from  w w  w .  j a v a2 s.  com*/
public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / (float) DisplayMetrics.DENSITY_MEDIUM);
}

From source file:Main.java

/**
 * Checks if the device is a tablet or a phone
 *
 * @param activityContext//w  w  w. ja va 2 s .  c o  m
 *          The Activity Context.
 * @return Returns true if the device is a Tablet
 */
public static boolean isTabletDevice(Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
    // If XLarge, checks if the Generalized Density is at least MDPI
    // (160dpi)
    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
        // DENSITY_TV=213, DENSITY_XHIGH=320
        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
            // Yes, this is a tablet!
            return true;
        }
    }
    // No, this is not a tablet!
    return false;
}