How to determine real content size of devices without status bar, action bar and navigation bar ?

  • Replies:5
Binh Le
  • Forum posts: 3

Mar 1, 2014, 5:36:11 AM via Website

I wanna create a method that is used for all app. As following

private int getRealContentSize(){
int statusBarHeight = Common.getStatusBarHeight(this);
int actionBarHeight = Common.getActionBarHeight(this);
int navigationBarHeight = Common.getNavigationBarHeight(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int totalHeight = statusBarHeight + actionBarHeight + navigationBarHeight;
Log.i("BAR", "status + " + statusBarHeight +
"; action bar + " + actionBarHeight +
"; navigation " + navigationBarHeight);
if(totalHeight < 50){
totalHeight = 50;
}
int thisHeight = metrics.heightPixels - totalHeight;
return thisHeight;
}

public static int getNavigationBarHeight(Context pContext){
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}

public static int getStatusBarHeight(Context pContext) {
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}

public static int getActionBarHeight(Context pContext) {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (pContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,pContext.getResources().getDisplayMetrics());
}
return actionBarHeight;
}

I tested on 2.3.3, 4.0.3, 4.4.2. It seems like still get error when displaying. Does anyone have other like this or having some methods can solve this problem ?

Thanks all

Reply
Binh Le
  • Forum posts: 3

Mar 1, 2014, 10:40:14 AM via Website

Uhm, maybe I can solve in some cases. The following is my solution, it has been tested on emulators: 2.2(320x480), 2.3.3(480x800), 3.0(480x800), 4.0.3(480x800), 4.2.2(480x800)

private int getRealContentSize(){
int statusBarHeight = Common.getStatusBarHeight(this);
int actionBarHeight = Common.getActionBarHeight(this);
int navigationBarHeight = Common.getNavigationBarHeight(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int totalHeight = 0;
if (android.os.Build.VERSION.SDK_INT < 14 /* 2.2<= x(320x480),(480x800) <= 4.0 */) {
totalHeight = (statusBarHeight + actionBarHeight + navigationBarHeight) * 2;
}
else if(android.os.Build.VERSION.SDK_INT >= 14 /* x(480x800) >= 4.0 */){
totalHeight = statusBarHeight + actionBarHeight;
}
int thisHeight = metrics.heightPixels - totalHeight;
return thisHeight;
}

If anyone has better solution, please share

Thanks,
Binh Le

— modified on Mar 1, 2014, 11:33:52 AM

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Mar 1, 2014, 11:24:44 AM via Website

Whats the Problem with my post?
it dos the same Thing you want.
It Returns she orginal Screen height, wich is built in the device.
But in your code i think, it won't work with every device

LG Pascal //It's not a bug, it's a feature. :) ;)

Reply
Binh Le
  • Forum posts: 3

Mar 1, 2014, 11:37:49 AM via Website

Hi Pascal P,

Seems like your post does not include case "dimension without action bar, status bar and navigation bar". Which I want is "dimension is used to display content".

Thanks,
Binh Le

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Mar 1, 2014, 12:04:35 PM via Website

ok, then look here:
http://stackoverflow.com/questions/1016896/how-to-get-screen-dimensions
It Displays the orginal Display size so it's without actionbar and Navigation bar.
You only get this infoprmation from the android System.
When you want to calc the Display size in a self coded function, it's more difficult then use the Information, the System gives to us.

LG Pascal //It's not a bug, it's a feature. :) ;)

Reply