Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.Activity;

import android.content.Context;

import android.graphics.Rect;

import android.view.Window;

public class Main {
    /**
     * get app bar height
     *
     * @param activity
     * @return
     */
    public static int getAppBarHeight(Activity activity) {
        return getContentTopHeight(activity) - getStatusBarHeight((Context) activity);
    }

    /**
     * get status bar plus app bar height, just content's({@link Window#ID_ANDROID_CONTENT}) top.
     *
     * @param activity
     * @return
     */
    public static int getContentTopHeight(Activity activity) {
        return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
    }

    /**
     * get status bar height.
     * <br />
     * rely on the fact that te status bar is shown at the time you make your computation,
     * <strong>
     * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
     * this will not work!!
     * </strong>
     *
     * @param activity
     * @return
     */
    public static int getStatusBarHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }

    /**
     * get status bar height from dimension resource called status_bar_height.
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}