Geek Quickies

Stories from the cloudvergence.

Get the Height of the Status Bar in Android

Sometimes in Android, the flexible layout system is not flexible enough and you need to make some computations inside your code. In these computations, you may need to subtract the size of the status bar. Stackoverflow gives you some answers, but they all rely on the fact that te status bar is shown at the time you make your computation. If you are in full screen mode, by having called for instance:

1
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

It doesn’t work.

The height of the status bar is contained in a dimension resource called status_bar_height. It’s not part of the public resources, so you can’t access it directly from your code with android.R.dimen.status_bar_height. You can however compute it at runtime with the following code:

1
2
3
4
5
6
7
8
public int getStatusBarHeight() {
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
      result = getResources().getDimensionPixelSize(resourceId);
  }
  return result;
}

You need to put this method in a ContextWrapper class.

Hope it helps.

Comments