1

Why does getting the effective screensize (screen-actionbar-statusbar) like below give different results different times? first time my fragment is started and calls getEffectiveScreenSize it gets it wrong (actionbar size is 0) but 2nd time and the rest it is correct.

So getting the actionbar size like below is not reliable, why? What is the corrrect and safe way?

public static Point getScreenSize( Activity theActivity )
{
    Display theDisplay = theActivity.getWindowManager().getDefaultDisplay();
    Point sizePoint = new Point();
    theDisplay.getSize( sizePoint );

    return sizePoint;
}

public static int getStatusBarHeight( Activity theActivity ) 
{
    int result = 0;
    int resourceId = theActivity.getResources().getIdentifier( "status_bar_height", "dimen", "android" );

    if (resourceId > 0) 
    {
        result = theActivity.getResources().getDimensionPixelSize( resourceId );
    }

    return result;
}

public static int getActionBarHeight( Activity theActivity )
{ 
    return theActivity.getActionBar().getHeight();
}

public static Point getEffectiveScreenSize( Activity theActivity )
{
    Point screenSize = getScreenSize( theActivity );
    screenSize.y = screenSize.y - getStatusBarHeight( theActivity ) - getActionBarHeight( theActivity );

    return screenSize;
}

This change did not help:

public static int getActionBarHeight( Activity theActivity )
{ 
    //return theActivity.getActionBar().getHeight();
    int result = 0;
    int resourceId = theActivity.getResources().getIdentifier( "actionbar_bar_height", "dimen", "android" );

    if (resourceId > 0) 
    {
        result = theActivity.getResources().getDimensionPixelSize( resourceId );
    }

    return result;
}

3 Answers 3

3

I'm not sure why all your methods are static, but more to the point for some reason you haven't included relevant code - where is getEffectiveScreenSize used?

The best answer I can give you has to be based on assumptions - you must be calling getEffectiveScreenSize at different points in the lifecycle - before, and after the layout has been measured and/or before the action bar is fully initialized (maybe look at the callbacks related to action bar).

If you want to have your layout account for the action bar there is a dimension you can use for that

?android:attr/actionBarSize

If you are trying to make or maybe stop the content and action bar overlapping then you probably have to change the value of action bar overlay in your styles

<item name="android:windowActionBarOverlay">bool</item>
0
1

Make sure that you are giving the action bar enough time to load before checking its size. I'm sure there is a best practices means of doing this, but for now just trying waiting a bit until it has loaded (you could try a loop that blocks until it is not size zero)

1
  • 1
    Even if it has a resource ID, I'm not sure that guarantees that it has finished inflating.
    – Matt
    Feb 27, 2014 at 23:55
1

The ActionBar's height probably is 0 when you measure it the first time. Wait until layout of the main view is finished, then you can measure the ActionBar and react.

final ViewTreeObserver vto = mainView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(
    new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Get the ActionBar height here
            vto.removeGlobalOnLayoutListener(this);
        }
    };
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.