2

I tried with:

Resources.getSystem().getDisplayMetrics().widthPixels

and:

Resources.getSystem().getDisplayMetrics().heightPixels

but when my app goes in standby for a long time, on the wake up the two lines above return 0.

EDIT: I would like to place these values into static and final field.

VanDir
  • 1,980
  • 3
  • 23
  • 41
  • Please see [this thread](http://stackoverflow.com/questions/1016896/android-how-to-get-screen-dimensions) – RyPope Apr 10 '13 at 20:47
  • Sorry I expressed myself badly, I would like to place this values into static and final field. – VanDir Apr 12 '13 at 17:36

2 Answers2

4

You can use a static initializer. You do that by embedding a block in your class body:

class MyClass {
   public static final int width;
   public static final int height;

   static {
       DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
       width = dm.widthPixels;
       height = dm.heightPixels;
   }
}
Paul de Vrieze
  • 4,888
  • 1
  • 24
  • 29
0

Here is what I use to get screen dimensions, you can put the display, screenWidth and screenHeight variables as class variables (outside any methods ie. onCreate) if you want. Then you can put the if statement into onCreate or another method to initialize the variables.

    Display display = getWindowManager().getDefaultDisplay();
    int screenWidth, screenHeight;

    if (android.os.Build.VERSION.SDK_INT >= 13)
    {
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
    }
    else
    {
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
    }
RyPope
  • 2,645
  • 27
  • 51
  • Sorry I expressed myself badly, I would like to place this values into static and final field. – VanDir Apr 20 '13 at 13:21