get Real Screen Size - Android Phone

Android examples for Phone:Screen

Description

get Real Screen Size

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static int[] getRealScreenSize(Context context) {

        int[] size = new int[2];

        WindowManager w = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display d = w.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        d.getMetrics(metrics);/* ww w  . j  ava 2s .c o m*/

        // since SDK_INT = 1;
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        // includes window decorations (statusbar bar/menu bar)
        if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
            try {
                widthPixels = (Integer) Display.class.getMethod(
                        "getRawWidth").invoke(d);
                heightPixels = (Integer) Display.class.getMethod(
                        "getRawHeight").invoke(d);
            } catch (Exception ignored) {
            }
        } else if (Build.VERSION.SDK_INT >= 17) {// includes window decorations (statusbar bar/menu bar)
            Point realSize = new Point();
            d.getRealSize(realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        }

        size[0] = widthPixels;
        size[1] = heightPixels;
        return size;
    }
}

Related Tutorials