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;
}
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>