Get offset of application content from top (so status bar + title bar). - Android User Interface

Android examples for User Interface:View Layer

Description

Get offset of application content from top (so status bar + title bar).

Demo Code


//package com.java2s;

import android.graphics.Rect;

import android.view.View;

import android.view.Window;

public class Main {
    private static Rect mStatusBarRect = new Rect();

    /**//from   w  w w .jav  a 2s .com
     * Get offset of application content from top (so status bar + title bar).<br>
     * <br>
     * <b>Note</b>: Call this when content is completely inflated. So {@code onCreate} of an
     * activity won't work.
     * 
     * @param view
     *            any view which is attached to the content
     * 
     * @return offset to top in pixel
     */
    public static int getContentOffsetFromTop(View view) {
        int offset = view.getRootView()
                .findViewById(Window.ID_ANDROID_CONTENT).getTop();

        if (offset == 0) {
            view.getWindowVisibleDisplayFrame(mStatusBarRect);
            offset = mStatusBarRect.top;
        }

        return offset;
    }
}

Related Tutorials