get Status Bar Height via reflection - Android android.app

Android examples for android.app:Status Bar

Description

get Status Bar Height via reflection

Demo Code

import java.lang.reflect.Field;

import android.content.Context;
import android.util.Log;

public class Main {

  private static final String TAG = "ActivityUtil";

  public static int getStatusBarHeight(final Context context) {
    Class<?> c = null;/*  w  ww .  j  a v  a2s  .  c  o m*/
    Object obj = null;
    Field field = null;
    int dp = 0;
    int statusBarHeight = 50;
    try {
      c = Class.forName("com.android.internal.R$dimen");
      obj = c.newInstance();
      field = c.getField("status_bar_height");
      dp = Integer.parseInt(field.get(obj).toString());
      statusBarHeight = context.getResources().getDimensionPixelSize(dp);
      Log.i(TAG, "Status height is: " + statusBarHeight);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return statusBarHeight;
  }

}

Related Tutorials