get Device Size Resources - Android Hardware

Android examples for Hardware:Device Feature

Description

get Device Size Resources

Demo Code


//package com.java2s;

import android.content.Context;
import android.content.res.Resources;

import android.util.DisplayMetrics;
import android.util.Log;

public class Main {
    private static final String TAG = "DeviceUtil";

    public static int getDeviceSize_Resources(Context context, int code) {
        Resources resources = context.getResources();
        DisplayMetrics outMetrics = resources.getDisplayMetrics();
        int size;

        float density = outMetrics.density;
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;

        switch (code) {
        case 0://w ww.j av  a  2 s  .com
            size = width;
            break;
        case 1:
            size = height;
            break;
        default:
            size = width;
            break;
        }

        Log.i(TAG, "Device size?Width:" + width + ",Height:" + height
                + ",Density:" + density);
        return size;
    }
}

Related Tutorials