Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.util.DisplayMetrics;

import android.view.WindowManager;

public class Main {
    /**
     * Get the current device physical size in inches.
     *
     * @param context valid context.
     * @return <b>double</b> device inches
     */
    public static double getDeviceDiagonalSizeInInches(final Context context) {
        final DisplayMetrics metrics = getDisplayMetrics(context);

        final double xInches = (double) metrics.widthPixels / metrics.xdpi;
        final double yInches = (double) metrics.heightPixels / metrics.ydpi;
        return Math.sqrt(Math.pow(xInches, 2) + Math.pow(yInches, 2));
    }

    /**
     * Returns a valid DisplayMetrics object
     *
     * @param context valid context
     * @return DisplayMetrics object
     */
    public static DisplayMetrics getDisplayMetrics(final Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }
}