get Screen Size from Context - Android User Interface

Android examples for User Interface:Screen Size

Description

get Screen Size from Context

Demo Code


//package com.java2s;

import android.content.Context;

import android.util.DisplayMetrics;

import android.view.WindowManager;

public class Main {
    public static final String UNKNOW = "Unknown";

    public static final String getScreenSize(Context context) {
        try {/*from w  w  w. j ava2  s . c o  m*/
            DisplayMetrics localDisplayMetrics = new DisplayMetrics();
            WindowManager localWindowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            localWindowManager.getDefaultDisplay().getMetrics(
                    localDisplayMetrics);
            return String.format("%dx%d", localDisplayMetrics.widthPixels,
                    localDisplayMetrics.heightPixels);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return UNKNOW;
    }
}

Related Tutorials