DisplayMetrics dm = getResources().getDisplayMetrics();
float fwidth = dm.density * dm.widthPixels;
float fheight = dm.density * dm.heightPixels;
If getSize
gets you an error due to your minSDKVersion and you don't want to use deprecated methods (getWidth
& getHeight
), the getMetrics
solution was originally posted on 2011 by Balaji.K... And Nik added a comment explaining getDisplayMetrics
also considers the status bar size.
Some other comments refer to multiply by the scale (density) in order to get the precise float value of the dimensions. Tested in Android v2.2 (API 8) and v4.0 with good results and no errors/warnings.