-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

Hugo OUKHAI
  • 29
  • 1
  • 4

1 Answers1

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,356
  • 1
  • 14
  • 31