I am trying to get the screen dimensions of an Android device, I am trying with this code:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // obsoleto (deprecated)
int height = display.getHeight(); // obsoleto (deprecated)
Which is obsolete (deprecated), what would be the current correct way to obtain the dimensions of the device screen?
You can use DisplayMetrics
Note: This form is supported from API Level 1
The correct thing to obtain the screen dimensions in Android and to work correctly in all APIs would be:
Another way would be using:
To access members
DisplayMetrics
, initialize an object like this:Another way would be:
You can do it this way:
Although the old way continues to work correctly.
You can also try using the getResources method automatically without importing new classes
I am new to the forum and thank you all for your multiple contributions regarding this topic I see that everything said works well but not in the case that I am developing a Custom component that extends LinearLayout or any ViewGroup if you want to take the screen dimensions from a class like the one I'm commenting on (then note that I'm not in the Activity but in an extended class of LinearLayout) Only this has worked for me: