Android programs a method to get the height of the notification bar

  • 2020-12-13 19:05:10
  • OfStack

This article illustrates the Android programming method for obtaining the height of the notification bar. To share for your reference, the details are as follows:

Here we get the height of the notification bar through the reflection mechanism

The height of the notification bar is written in the dimen file:


public static int getStatusBarHeight(Context context){
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
      c = Class.forName("com.android.internal.R$dimen");
      obj = c.newInstance();
      field = c.getField("status_bar_height");
      x = Integer.parseInt(field.get(obj).toString());
      statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    return statusBarHeight;
}

I hope this article has been helpful in Android programming.


Related articles: