Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen Dimensions in Android [duplicate]

Tags:

android

As there are two methods for getting size. From API level above 13 this method works for me

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

And below 14 this method works for me

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Can i get screen size for all API levels??? Kindly help me.

like image 959
user3041975 Avatar asked Nov 10 '22 10:11

user3041975


1 Answers

try with this dude

int density;
    private DisplayMetrics metrics;
    private int widthPixels;
    private float scaleFactor;
    private float widthDp;
private DisplayMetrics metrics;
          metrics = new DisplayMetrics();
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

     density= getResources().getDisplayMetrics().densityDpi;

     widthPixels = metrics.widthPixels;

     scaleFactor = metrics.density;

     widthDp = widthPixels / scaleFactor;
like image 59
Bhanu Sharma Avatar answered Nov 15 '22 01:11

Bhanu Sharma