get StatusBar Height via Reflection - Android User Interface

Android examples for User Interface:StatusBar

Description

get StatusBar Height via Reflection

Demo Code


//package com.java2s;
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  w  w  .j  a  va  2s. 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