11

this is a really simple question on which I've found no answer :/ How can I quickly access the screen resolution (width, height) as integer values?

I've tried this one, but it always shows zero on my emulator:

DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;

In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.

Can someone give me a hint?

poeschlorn
  • 12,230
  • 17
  • 54
  • 65
  • just a guess - the values retrieved from DisplayMetrics may not be valid until first draw. are you checking this on onCreate or onDraw or when? – SteelBytes May 25 '10 at 07:13
  • The real reason why the result was zero is because zero is the default value in java for an int. `dm.widthPixels` was not initialized so it's value is zero and zero divided by two is also zero... you should have tried something like `mView.getContext().getResources().getDisplayMetrics()` – Armin Aug 05 '16 at 17:16

5 Answers5

36

The trashkalmar answer is correct.

However, the results will be specific to the activity context. If you want the whole device screen resolution:

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;

Note that the results also depend on the current device orientation.

Guillaume Perrot
  • 4,278
  • 3
  • 27
  • 37
18

You can get screen metrics in this way:

Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
trashkalmar
  • 883
  • 5
  • 11
  • 1
    I get a @deprecated warning, with no further explanation. used Guillaume Perrot's answer instead – Moak Aug 08 '12 at 01:34
  • @Moak, it was deprecated since Android API 13. See the [docs](http://developer.android.com/reference/android/view/Display.html#getWidth%28%29). – Aritz Oct 28 '15 at 10:12
8

1.Simply use This inside activity to get screen width and height pixels.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
    .getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

2.This can also be used But requires Api level inlined check

Display display = getWindowManager().getDefaultDisplay();

Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    display.getSize(size);
    int width2 = size.x;
    int height2 = size.y;

} else {
    int width2 = display.getWidth();
    int height2 = display.getHeight();
}

3.Use This when you are not in activity but having context

WindowManager wm = (WindowManager) context.getSystemService(
            Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();

int width1;
int height1;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

    display1.getSize(size1);
    width1 = size1.x;
    height1 = size1.y;

} else {
    width2 = display.getWidth();
    height2 = display.getHeight();
}
Aritz
  • 30,971
  • 16
  • 136
  • 217
DeepakPanwar
  • 1,389
  • 14
  • 22
4

I use the following:

int scrWidth  = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();

No muss, no fuss, just 2 class variables

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Rich
  • 4,157
  • 5
  • 33
  • 45
3

The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • so what does this mean for me, when I dynamically want to add TableRows and after finishing add the table to the main LinearLayout and set this one to ContentView (by using this.setContentView(ll), where ll is my linearLayout? – poeschlorn May 25 '10 at 09:13
  • onSizeChanged will be called the first time your View is drawn and whenever the orientation changes. When it is called, store the dimensions of the screen in members of your classes. Use these values when you are drawing (in the OnDraw method). – kgiannakakis May 25 '10 at 09:18