get Device Dimensions - Android Hardware

Android examples for Hardware:Device Feature

Description

get Device Dimensions

Demo Code


//package com.java2s;

import android.content.Context;
import android.graphics.Point;

import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.os.Build;

public class Main {
    public static Point getDeviceDimensions(Context context) {
        Point result = new Point();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        if (Build.VERSION.SDK_INT >= 11) {
            try {
                display.getRealSize(result);
                return result;
            } catch (NoSuchMethodError e) {
                result.x = display.getWidth();

                result.y = display.getHeight();
            }// ww  w.ja v  a 2 s.  com
        } else {
            DisplayMetrics metrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            result.x = metrics.widthPixels;
            result.y = metrics.heightPixels;
        }
        return result;
    }
}

Related Tutorials