34

I have two android device with same resolution

Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches

Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches

How to find device diagonal screen size?

Detect 7 inch and 10 inch tablet programmatically

I have used the above link but it gives both device diagonal screen size -> 5.8

Community
  • 1
  • 1
Kailas
  • 3,173
  • 5
  • 42
  • 52

7 Answers7

88

try this code to get screen size in inch

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);
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
Ali Ahmad
  • 1,351
  • 9
  • 11
  • 7
    Not getting exact location. if screen size 5.00 inches then get 4.338 value – Vrajesh Sep 23 '15 at 15:01
  • 9
    `double wi=(double)width/dm.xdpi; double hi=(double)height/dm.ydpi;` is more accurate. According to the docs for DensityMetrics, it gives the exact density, while `dm.densityDpi` can only give certain discrete values. I've verified it working on two devices. – Matthew Grivich Nov 03 '15 at 01:22
  • 4
    it doesn't accurate. it returns 4.3" for my 4.7" phone – Shayan_Aryan Dec 22 '15 at 14:51
  • 1
    If you're getting weird answers (or more importantly your images come out weird because you are using the size to scale stuff), don't forget to use scaledDensity. See my answer below. – Moe Singh Jan 05 '17 at 21:43
  • 1
    For SDK 17& above refer https://stackoverflow.com/a/55337830/6503228 for accurate screensize – devgun Mar 25 '19 at 12:31
  • Returns 5.7" for Google Pixel 2 XL when it is really 6" – Johann Aug 11 '21 at 12:14
12

This won't work?

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); 
8

Don't forget to multiply the screen size by the scaledDensity if you are doing what I did and change the size of stuff based on the screen size. e.g.:

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) * dm.scaledDensity;
Log.d("debug", "Screen inches : " + screenInches); 

Note the second last line! Here's more info the scaledDensity stuff: What does DisplayMetrics.scaledDensity actually return in Android?

Community
  • 1
  • 1
Moe Singh
  • 809
  • 7
  • 12
  • The accepted answer to the question you linked also mentioned using `getResources().getDisplayMetrics();` to get the correct scaledDensity value. – jk7 Jul 26 '17 at 15:19
  • * dm.scaledDensity saved my day. Thanks. – masgar Feb 01 '18 at 16:45
7

None of the above answers gave correct screen size... But the below method does it right.

private String getScreenSize() {
    Point point = new Point();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealSize(point);
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int width=point.x;
    int height=point.y;
    double wi=(double)width/(double)displayMetrics.xdpi;
    double hi=(double)height/(double)displayMetrics.ydpi;
    double x = Math.pow(wi,2);
    double y = Math.pow(hi,2);
    return String.valueOf(Math.round((Math.sqrt(x+y)) * 10.0) / 10.0);
}

Note: This works only on API 17 & above.

devgun
  • 1,003
  • 13
  • 33
5

Try this:

 public static Boolean isTablet(Context context) {

    if ((context.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {

      return true;
  }
    return false;
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Niveditha
  • 148
  • 1
  • 8
4
DisplayMetrics displayMetrics = context.getResources()
                    .getDisplayMetrics();

String screenWidthInPix = displayMetrics.widthPixels;

String screenheightInPix = displayMetrics.heightPixels;
4

Pythagoras theorem to find the diagonal size of Android phone/tablet screen, same principal can be applied to iPhone or Blackberry screen.

Try as below the other way:

  DisplayMetrics met = new DisplayMetrics();                
  this.getWindowManager().getDefaultDisplay().getMetrics(met);// get display metrics object
  String strSize = 
  new DecimalFormat("##.##").format(Math.sqrt(((met.widthPixels / met.xdpi) *
  (met.widthPixels / met.xdpi)) +
  ((met.heightPixels / met.ydpi) * (met.heightPixels / met.ydpi))));
  // using Dots per inches with width and height
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • this.getWindowManager() this gives me error. how can get this. i can get diaplay matrics by these view.getContext().getResources().getDisplayMetrics(); – Kailas Oct 03 '13 at 10:30
  • The method getWindowManager() is undefined for the type homeFragment – Kailas Oct 03 '13 at 11:18
  • I write these way. Is any thing wrong in it b'coz it gives me wrong diagonal screen size. DisplayMetrics met = view.getContext().getResources().getDisplayMetrics();// get display metrics object String strSize = new DecimalFormat("##.##").format(Math.sqrt(((met.widthPixels / met.xdpi) * (met.widthPixels / met.xdpi)) + ((met.heightPixels / met.ydpi) * (met.heightPixels / met.ydpi)))); Log.d("Sreen Inches0", "Inches0:"+strSize); – Kailas Oct 03 '13 at 11:21
  • The method `getWindowManager()` needs instance of activity so you have to write `Activity.getWindowManager()`. – GrIsHu Oct 03 '13 at 11:22