intA
intA

Reputation: 2691

How do I get screen height minus the actionbar?

When I have the phone in landscape I'm wondering how I can get the pixel height of the screen not including the actionbar. So the pixel height of the actual viewable area. I want this so I can size a GridLayout appropriately. The GridLayout is inside of a LinearLayout so if I could get the pixel height of the LinearLayout that should work too.

Thanks!

Upvotes: 1

Views: 2171

Answers (2)

azizbekian
azizbekian

Reputation: 62189

You may use View#getGlobalVisibleRect(Rect) to get the coordinates of a particular view on the screen.


    layout.post(new Runnable() {
        @Override
        public void run() {
            final Rect rect = new Rect();
            layout.getGlobalVisibleRect(rect);

            final int height = rect.height();
        }
    });

Upvotes: 0

AndrewS
AndrewS

Reputation: 3109

Calculate the entire height:

public static int getScreenHeight(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics.heightPixels;
}

and deduct Action Bar height from it:

public static int getActionBarHeight(Context context) {
    int[] textSizeAttr = new int[]{R.attr.actionBarSize};
    TypedArray a = context.obtainStyledAttributes(new TypedValue().data,  textSizeAttr);
    int height = a.getDimensionPixelSize(0, 0);
    a.recycle();
    return height;
}

Upvotes: 4

Related Questions