Obtain current mobile phone screen pixels' size - Android User Interface

Android examples for User Interface:Screen Size

Description

Obtain current mobile phone screen pixels' size

Demo Code


//package com.java2s;

import android.content.Context;

import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Main {
    /**/*  w  w  w.ja v a  2 s .  c o m*/
     * Obtain current mobile phone screen pixels' size,such as :
     * <pre>
     * int size[] = DeviceUtility.obtainDeviceSize(context);
     * int width = size[0];
     * int height = size[1];
     * </pre>
     * @param context
     * @return
     */
    public static final int[] obtainDeviceSize(Context context) {
        int[] size = new int[2];
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        size[0] = width;
        size[1] = height;
        return size;
    }
}

Related Tutorials