Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

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

Prototype

public DisplayMetrics() 

Source Link

Usage

From source file:Main.java

public static void getDisplayDpi(Context ctx) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
    double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);
    int screenInch = (int) Math.round(screenInches);
    Log.d("screeninch", String.valueOf(screenInch));
    int dapi = dm.densityDpi;
    Log.d("dapi", String.valueOf(dapi));
    try {//from  w  ww  .j  a  v a  2  s  .co m
        switch (dm.densityDpi) {

        case DisplayMetrics.DENSITY_LOW:
            UI_DENSITY = 120;
            if (screenInch <= 7) {
                UI_SIZE = 4;

            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            UI_DENSITY = 160;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;
        case DisplayMetrics.DENSITY_HIGH:
            UI_DENSITY = 240;
            if (screenInch <= 7) {
                UI_SIZE = 4;
            } else {
                UI_SIZE = 10;
            }
            break;

        default:
            break;
        }
    } catch (Exception e) {
        // Caught exception here
    }
}

From source file:Main.java

public static float getScreenDensity(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);/*from w  w w.j av  a2  s .c om*/
    return metrics.density;
}

From source file:Main.java

/**
 * Reports to the log information about the device's display. This information includes
 * the width and height, and density (low, medium, high). 
 * @param activity The Activity to report on.
 *//*from  w w  w.j  ava2s  .  c  om*/
public static void reportDisplayInformation(Activity activity) {

    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int displayWidth = metrics.widthPixels;
    int displayHeight = metrics.heightPixels;

    String density = "unknown";
    switch (metrics.densityDpi) {
    case DisplayMetrics.DENSITY_LOW:
        density = "Low";
        break;
    case DisplayMetrics.DENSITY_MEDIUM:
        density = "Medium";
        break;
    case DisplayMetrics.DENSITY_HIGH:
        density = "High";
        break;
    }

    Log.i(TAG, "Display is " + displayWidth + "x" + displayHeight + ", Density: " + density);

}

From source file:Main.java

/**
 * Because of a BUG of Android (API 13-),
 * get signature info by using "getPackageArchiveInfo" of "PackageManager"
 * always causes "NullPointerException".
 * Lack of code in method "getPackageArchiveInfo":
 *     if ((flags & GET_SIGNATURES) != 0)
 *     {/*from  w  w w .ja va 2s.com*/
 *         packageParser.collectCertificates(pkg, 0);
 *     }
 */
@SuppressWarnings("unchecked")
public static PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
    try {
        Class packageParserClass = Class.forName("android.content.pm.PackageParser");
        Class[] innerClasses = packageParserClass.getDeclaredClasses();
        Class packageParserPackageClass = null;
        for (Class innerClass : innerClasses) {
            if (0 == innerClass.getName().compareTo("android.content.pm.PackageParser$Package")) {
                packageParserPackageClass = innerClass;
                break;
            }
        }
        Constructor packageParserConstructor = packageParserClass.getConstructor(String.class);
        Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class,
                String.class, DisplayMetrics.class, int.class);
        Method collectCertificatesMethod = packageParserClass.getDeclaredMethod("collectCertificates",
                packageParserPackageClass, int.class);
        Method generatePackageInfoMethod = packageParserClass.getDeclaredMethod("generatePackageInfo",
                packageParserPackageClass, int[].class, int.class, long.class, long.class);
        packageParserConstructor.setAccessible(true);
        parsePackageMethod.setAccessible(true);
        collectCertificatesMethod.setAccessible(true);
        generatePackageInfoMethod.setAccessible(true);

        Object packageParser = packageParserConstructor.newInstance(archiveFilePath);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        displayMetrics.setToDefaults();

        final File sourceFile = new File(archiveFilePath);

        Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, displayMetrics, 0);
        if (pkg == null) {
            return null;
        }

        if ((flags & PackageManager.GET_SIGNATURES) != 0) {
            collectCertificatesMethod.invoke(packageParser, pkg, 0);
        }

        return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.csware.ee.utils.Tools.java

public static int getScreenWidth(Activity context) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}

From source file:com.thevelopment.poc.Fragments.FirstFragment.java

private void renderScreen(View view) {

    RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.fragment_first_relative_layout_id);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;

    LinearLayout innerLinearLayout = createInnerLinearLayout(relativeLayout, width, height);

    companyEditText = createField(innerLinearLayout, "Garagem/Empresa", height);
    prefixEditText = createField(innerLinearLayout, "Prefixo", height);
    licenseEditText = createField(innerLinearLayout, "Placa", height);

    LinearLayout typeAndDateLinearLayout = screenBuilder.createRowLayout(innerLinearLayout);

    typeSpinner = createTypeOfInspectionField(typeAndDateLinearLayout, width);

    FloatingActionButton fab = createFAB(innerLinearLayout);
    setFABListener(fab);/*from   ww w. ja va2  s .  c o m*/
}

From source file:net.genus.plugins.screensize.ScreenSize.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("getSize")) {
        Log.d("ScreenSizeDebug", "getSize");
        DisplayMetrics metrics = new DisplayMetrics();
        Context context = this.cordova.getActivity().getApplicationContext();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);

        Log.d("ScreenSizeDebug", "Pixles wide: " + metrics.widthPixels);
        Log.d("ScreenSizeDebug", "Pixels high: " + metrics.heightPixels);
        Log.d("ScreenSizeDebug", "DPI Density: " + metrics.densityDpi);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        int dens = metrics.densityDpi;

        double wi = (double) width / (double) dens;
        double hi = (double) height / (double) dens;

        Log.d("ScreenSizeDebug", "Width:  " + wi);
        Log.d("ScreenSizeDebug", "Height: " + hi);

        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);

        double screenInches = Math.sqrt(x + y);
        Log.d("ScreenSizeDebug", "Screen size: " + screenInches);

        screenInches = screenInches * 1000;

        int result = (int) screenInches;
        Log.d("ScreenSizeDebug", "" + result);

        callbackContext.success(result);

        return true;
    }/* w  w  w.  j  av  a  2s.  c  o m*/
    return false;
}

From source file:com.spoiledmilk.ibikecph.util.Util.java

public static void init(WindowManager manager) {
    if (jsonMapper == null) {
        jsonMapper = new ObjectMapper();
    }//from  w w w  .jav a 2s.  co m

    if (metrics == null) {
        metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        if (metrics != null) {
            screenWidht = metrics.widthPixels;
            screenHeight = metrics.heightPixels;
            LOG.d("screenWidth = " + screenWidht);
            LOG.d("screenHeight = " + screenHeight);
            LOG.d("density = " + metrics.density);
        }
    }
}

From source file:ch.rts.cordova.is.tablet.IsTablet.java

private boolean isTabletDevice(Context applicationContext) {
    boolean device_large = ((applicationContext.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);

    if (device_large) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = this.cordova.getActivity();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        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) {
            Log.d(LOG_TAG, "Is Tablet Device");
            return true;
        }//from  w w  w.ja v a 2  s.c  o  m
    }
    Log.d(LOG_TAG, "Is NOT Tablet Device");
    return false;
}

From source file:ly.count.android.api.DeviceInfoTests.java

public void testGetResolution() {
    final DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
            .getMetrics(metrics);/*  w w  w.j  a va2 s .  c  o  m*/
    final String expected = metrics.widthPixels + "x" + metrics.heightPixels;
    assertEquals(expected, DeviceInfo.getResolution(getContext()));
}