get Resolution As String - Android App

Android examples for App:Resource

Description

get Resolution As String

Demo Code


//package com.java2s;

import java.lang.reflect.Method;
import android.content.Context;

import android.os.Build;

import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static String getResolutionAsString(Context context) {
        int widthPixels = 0;
        int heightPixels = 0;
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        if (Build.VERSION.SDK_INT <= 12) {
            widthPixels = display.getWidth();
            heightPixels = display.getHeight();
        } else {/*  ww  w .j a  v a  2  s  .co  m*/
            try {
                Method methodW = Display.class.getMethod("getRawWidth");
                widthPixels = (Integer) methodW.invoke(display);

                Method methodH = Display.class.getMethod("getRawHeight");
                heightPixels = (Integer) methodH.invoke(display);
            } catch (Exception e) {
                e.printStackTrace();
                widthPixels = display.getWidth();
                heightPixels = display.getHeight();
            }
        }
        return widthPixels < heightPixels ? widthPixels + "x"
                + heightPixels : heightPixels + "x" + widthPixels;
    }
}

Related Tutorials