get StatusBar Height, check if it is phone - Android User Interface

Android examples for User Interface:StatusBar

Description

get StatusBar Height, check if it is phone

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import android.content.Context;

import android.graphics.Point;

import android.view.Display;

import android.view.WindowManager;

public class Main {
    private static Context context;
    private static boolean phone;

    public static int getStatusBarHeight() {
        int result = 0;

        if (isPhone() || !hasOnScreenSystemBar()) {
            int resourceId = context.getResources().getIdentifier(
                    "status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(
                        resourceId);//from ww  w.j  a v a  2  s.c o  m
            }
        }

        return result;
    }

    public static boolean isPhone() {
        return phone;
    }

    public static boolean hasOnScreenSystemBar() {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display d = wm.getDefaultDisplay();
        int deviceDisplayHeight = 0;
        try {
            Method getRawHeight = Display.class.getMethod("getRawHeight");
            deviceDisplayHeight = (Integer) getRawHeight.invoke(d);
        } catch (Exception ex) {

        }

        Point s = new Point();
        d.getSize(s);
        int windowHeight = s.y;

        return deviceDisplayHeight - windowHeight > 0;
    }
}

Related Tutorials