Displays a toast message stating the density of this device. - Android User Interface

Android examples for User Interface:Toast

Description

Displays a toast message stating the density of this device.

Demo Code


//package com.java2s;

import android.app.Activity;

import android.util.DisplayMetrics;
import android.widget.Toast;

public class Main {
    public final static String[] DENSITIES = { "low", "medium", "high",
            "xhigh", "xxhigh" };

    /**//ww w .j a v a  2s.c  o  m
     * Displays a toast message stating the density of this device. One of
     * four options:
     * low, medium, high, xhigh
     * 
     * @param context
     */
    public static void showDensity(Activity context) {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int density = metrics.densityDpi;
        String dense = "unknown";
        switch (density) {
        case DisplayMetrics.DENSITY_LOW:
            dense = DENSITIES[0];
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            dense = DENSITIES[1];
            break;
        case DisplayMetrics.DENSITY_HIGH:
            dense = DENSITIES[2];
            break;
        // DisplayMetrics.DENSITY_XHIGH == 320. This is to support APIs less
        // than 9.
        case 320:
            dense = DENSITIES[3];
            break;
        // DisplayMetrics.DENSITY_XXHIGH == 480. This is to support APIs less than
        // 16            
        case 480:
            dense = DENSITIES[4];
            break;
        }
        Toast.makeText(context, "Screen density: " + dense,
                Toast.LENGTH_LONG).show();
    }
}

Related Tutorials