Danilo Setton
Danilo Setton

Reputation: 713

Android Display x DisplayMetrics - What's the most correct way to access the display dimensions?

1st Method:

DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();

int deviceHeight = displayMetrics.heightPixels;
int deviceWidth = displayMetrics.widthPixels;

2nd Method:

Display mDisplay = getWindowManager().getDefaultDisplay();
Point point = new Point();
mDisplay.getSize(point);

int deviceHeight = point.y;
int deviceWidth = point.x;

I made some tests and both methos return the same value. So, the method I use depends on the context I'm using it? Or, is one method better then the other?

Thanks,

Upvotes: 2

Views: 79

Answers (1)

Alex
Alex

Reputation: 547

DisplayMetrics is only used to grab information about the display such as size, density, and font scaling.

On the other hand, the Display class can do the same thing but also get the size of the display that's not showing an application (Not including system decorations) or get the part of the display that is showing an application (Including system decorations).

It seems like if all you want to do is get the display size, DisplayMetrics would be the way to go because it is simplest for your needs. There is no better, just preference.

Upvotes: 1

Related Questions