get Real Display Height - Android User Interface

Android examples for User Interface:Display

Description

get Real Display Height

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import android.content.Context;

import android.os.Build;

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

public class Main {
    public static int getRealDisplayH(Context context) {
        Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();/*from   w  w  w.j  a v a  2s  .  com*/
        int realHeight;

        if (Build.VERSION.SDK_INT >= 17) {
            // new pleasant way to get real metrics
            DisplayMetrics realMetrics = new DisplayMetrics();
            display.getRealMetrics(realMetrics);
            realHeight = realMetrics.heightPixels;

        } else if (Build.VERSION.SDK_INT >= 14) {
            // reflection for this weird in-between time
            try {
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                realHeight = (Integer) mGetRawH.invoke(display);
            } catch (Exception e) {
                realHeight = display.getHeight();
            }

        } else {
            realHeight = display.getHeight();
        }
        return realHeight;
    }
}

Related Tutorials