get Screen Height Without Titlebar - Android User Interface

Android examples for User Interface:TitleBar

Description

get Screen Height Without Titlebar

Demo Code


//package com.java2s;

import android.content.Context;

import android.util.DisplayMetrics;

import android.view.WindowManager;

import java.lang.reflect.Field;

public class Main {
    public static int getScreenHeightWithoutTitlebar(Context ctx) {
        int[] screenWidthAndHeight = getScreenWidthAndHeight(ctx);
        return screenWidthAndHeight[1] - getStatusBarHeight(ctx)
                - dip2px(ctx, 48);//from  w w  w  .  ja v a2  s .com
    }

    public static int[] getScreenWidthAndHeight(Context ctx) {
        WindowManager mWm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);

        DisplayMetrics dm = new DisplayMetrics();
        // ???????????
        mWm.getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeigh = dm.heightPixels;

        return new int[] { screenWidth, screenHeigh };
    }

    public static int getStatusBarHeight(Context ctx) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = ctx.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sbar;
    }

    /**
     * dp2px
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

Related Tutorials