-1

I want to get the size of the screen, I already have the resolution

 DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager windowmanager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
    final int deviceWidth = displayMetrics.widthPixels;
    final int deviceHeight = displayMetrics.heightPixels;

but I want the size in inches, like the Samsung Galaxy S7 has a resolution of 2560x1440 and a screen of 5.1" , it's the 5.1 I want to get.

Thanks

2 Answers2

3

If you want the display dimensions in pixels you can use this code:

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

Then you can add condition that compares the height to satisfy your needs.

In inches:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);
Rhn Bhadani
  • 2,208
  • 1
  • 17
  • 25
2

Try this

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
double wi=(double)width/(double)dm.xdpi;
double hi=(double)height/(double)dm.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
Anmol317
  • 1,336
  • 1
  • 13
  • 31