3

I want to calculate device size by inch. I am using this code that every search appears. But the problem is when I put device 4.5 inch my answer in android studio is 4. I tried 5.2 inch device and I got 4.3 inch as well 10.1 inch -> the answer is 9.0. So how can I get accurate answer?

 DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    double wi=(double)dm.widthPixels/dm.xdpi;
    double hi=(double)dm.heightPixels/dm.ydpi;
    double x = Math.pow(wi,2);
    double y = Math.pow(hi,2);
    double screenInches = Math.sqrt(x+y);
    Log.e("hello"," "+screenInches);
Kuldeep Kulkarni
  • 796
  • 5
  • 20

4 Answers4

2

The problem with your code is it does not take into account

Actionbar

Softkeys

Orientation of the screen

use the following answer

 WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);

// since SDK_INT = 1;
mWidthPixels = displayMetrics.widthPixels;
mHeightPixels = displayMetrics.heightPixels;

// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
{
    try
    {
        mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
        mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
    }
    catch (Exception ignored)
    {
    }
}

// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
{
    try
    {
        Point realSize = new Point();
        Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
        mWidthPixels = realSize.x;
        mHeightPixels = realSize.y;
    }
    catch (Exception ignored)
    {
    }


 DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(mWidthPixels/dm.xdpi,2);
double y = Math.pow(mHeightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);

credit to him

Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • thanks for the answer and I already tried this one too... i searched alot and tried alot before i wrote it here –  Apr 07 '17 at 05:53
1

You can use getResources().getDisplayMetrics()

DisplayMetrics: A structure describing general information about a display, such as its size, density, and font scaling.

DisplayMetrics metrics = getResources().getDisplayMetrics();

    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    int dens = metrics.densityDpi;
    double wi = (double)width / (double)dens;
    double hi = (double)height / (double)dens;
    double x = Math.pow(wi, 2);
    double y = Math.pow(hi, 2);
    double screenInches = Math.sqrt(x+y);

    System.out.println("getSIZE"+screenInches);

For more information visit Android DisplayMetrics returns incorrect screen size.

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Device Size is different from screen size as screen will not cover total device size

Android OS has nothing to do with device size you cannot programatically get device dimensions you can get only screen dimensions

nithin y.n.v
  • 158
  • 2
  • 14
0

Try This With Just Small Change

DisplayMetrics metrics = getResources().getDisplayMetrics();

int width = metrics.widthPixels;
int height = metrics.heightPixels;
int dens = metrics.densityDpi;
double wi = (double)width / (double)dens;
double hi = (double)height / (double)dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
try {
double screenInches = Double.parseDouble(df.format(Math.sqrt(x+y)));

Log.e("hello"," "+screenInches);
} catch (NumberFormatException e) {

e.printStackTrace();

}
Abhishek
  • 3,348
  • 3
  • 15
  • 34
  • thank for the answer..! but it is the same still i got 4.08 for 4.5 inch screen –  Apr 07 '17 at 06:14
  • Not possible. How you got two digits after decimal point when you take in format as only single digit after decimal point. – Abhishek Apr 07 '17 at 06:23
  • Put your log response so I will get some idea why it happens. – Abhishek Apr 07 '17 at 06:24
  • 04-07 10:33:22.375 4775-4775/com.tsd.speakUp E/hello: 4.1 –  Apr 07 '17 at 06:33
  • You will always get approx values – Abhishek Apr 07 '17 at 06:40
  • if it was approx why 5.2 inch gives me 4.3 inch –  Apr 07 '17 at 06:48
  • I will check for any external lib and let you know. – Abhishek Apr 07 '17 at 06:52
  • thanks actually i tried eveything and still can't get what i want and by the way my width and height is right but that math things gives the wrong answers –  Apr 07 '17 at 06:55
  • Math gives right answer but issue is manufacturing company will gives you standard size as specification but it will differ for different manufacture. Let say some specification shows 7 inch but actually hardware of display provides 6.7 ~ 6.8 something like that. – Abhishek Apr 07 '17 at 08:48
  • i get it but it is not close as u say like 5.0 ,5.2 inch screens gives 4.4 5.5-->4.6 , 5.2-->4.6 , 10.1 -->9.1 –  Apr 07 '17 at 10:32
  • 1
    I also got same its not even close. – Abhishek Apr 07 '17 at 10:58