سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

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?

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.

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;
}
DisplayMetrics displayMetrics = context.getResources()
                    .getDisplayMetrics();

String screenWidthInPix = displayMetrics.widthPixels;

String screenheightInPix = displayMetrics.heightPixels;

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top