Android Screen Size Get getScreenSizePixels(Context ctx)

Here you can find the source of getScreenSizePixels(Context ctx)

Description

get Screen Size Pixels

Declaration

public static int[] getScreenSizePixels(Context ctx) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static int[] getScreenSizePixels(Context ctx) {
        int widthHeightInPixels[] = new int[2];
        try {//from   w  w w. j  a va2 s . c  o  m
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                Resources resources = ctx.getResources();
                Configuration config = resources.getConfiguration();
                DisplayMetrics dm = resources.getDisplayMetrics();
                double screenWidthInPixels = (double) config.screenWidthDp
                        * dm.density;
                double screenHeightInPixels = screenWidthInPixels
                        * dm.heightPixels / dm.widthPixels;
                widthHeightInPixels[0] = (int) (screenWidthInPixels + .5);
                widthHeightInPixels[1] = (int) (screenHeightInPixels + .5);
            } else {
                WindowManager wm = (WindowManager) ctx
                        .getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                widthHeightInPixels[0] = display.getWidth(); // deprecated
                widthHeightInPixels[1] = display.getHeight(); // deprecated
            }
        } catch (Exception e) {
        }
        return widthHeightInPixels;
    }
}

Related

  1. getScreenHeight(Display display)
  2. getScreenPixel(Activity activity)
  3. getScreenWidth(Context context)
  4. getScreenWidth(Context context)
  5. getScreenWidth(Display display)
  6. getRealScreenDimensions(Context context)
  7. getScreenHeight(Context context)
  8. getScreenSize(@Nonnull Context context)
  9. getScreenWidth(Context context)