get Current Screen Width Inches - Android Phone

Android examples for Phone:Screen

Description

get Current Screen Width 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 getCurrentScreenWidthInches(Context context) {
        DisplayMetrics dm = getDisplayMetrics(context);
        return dm.widthPixels / dm.xdpi;
    }/*w  ww  .  ja  v a  2  s.  co m*/

    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