get Diagonal Screen Inches - Android User Interface

Android examples for User Interface:Screen Size

Description

get Diagonal Screen Inches

Demo Code


//package com.java2s;
import android.content.Context;

import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static double getDiagonalScreenInches(Context context) {
        DisplayMetrics dm = getDisplayMetrics(context);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        return Math.sqrt(x + y);
    }/*from  ww w .j  a v  a  2 s .  c om*/

    private static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        getDefaultDisplay(context).getMetrics(dm);
        return dm;
    }

    private static Display getDefaultDisplay(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay();
    }
}

Related Tutorials