Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayMetrics returning all zero

Tags:

android

I am trying to access some DisplayMetrics data and everything is returning zero. This is how I am initializing display metrics object:

DisplayMetrics display = new DisplayMetrics();

and when I call display.heightPixels it's just zero. Do I need a permission or something to access DisplayMetrics?

like image 492
nkcmr Avatar asked Dec 21 '22 09:12

nkcmr


1 Answers

This is not how to use DisplayMetrics.

This is how:

final WindowManager w = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
final Display d = w.getDefaultDisplay();
final DisplayMetrics m = new DisplayMetrics();
d.getMetrics(m);

// m now contains reliable data
like image 62
OcuS Avatar answered Jan 04 '23 08:01

OcuS