get Screen Config - Android Phone

Android examples for Phone:Screen

Description

get Screen Config

Demo Code


//package com.java2s;
import android.content.Context;

import android.view.WindowManager;
import java.lang.reflect.Field;

public class Main {
    public static int screenWidth = 0;
    public static int screenHeight = 0;
    public static int statusBar = 0;

    public static void getScreenConfig(Context context) {

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        Class<?> c = null;/*from   www . j a v  a2  s .co m*/
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 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());
            sbar = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        screenWidth = width;
        screenHeight = height - sbar;
        statusBar = sbar;
    }
}

Related Tutorials