Android UI How to - Get Status Bar Height








Question

We would like to know how to get Status Bar Height.

Answer

//w  w w.j  av  a2s .  c  o m

import java.lang.reflect.Field;

import android.app.Activity;

public class Main {
  public static int getStatusBarHeight(Activity activity) {
    try {
      Class<?> clazz = Class.forName("com.android.internal.R$dimen");
      Object object = clazz.newInstance();
      Field field = clazz.getField("status_bar_height");
      int dpHeight = Integer.parseInt(field.get(object).toString());
      return activity.getResources().getDimensionPixelSize(dpHeight);
    } catch (Exception e1) {
      e1.printStackTrace();
      return 0;
    }
  }
}