-3

I have tried the codes below for finding out the screen size but none worked for me. The interface also goes out of proportion for different devices of the same screen sizes and same dimensions. I don't have actual devices, I am using the devices with screen sizes/dimensions provided by the AVD. The codes work for dimensions but it doesn't give the correct screen size. The screen size it calculates for a 4 inch screen is 3.9 inches (round figure) and the screen size for a 3.7 inch screen is also 3.9 inches. The interface code for the Nexus S (4", 480x800: hdpi) doesn't work for the 4" WVGA (480x800: hdpi) where both have the same screen size and same dimensions.

Could you please help me solve this problem.

DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int widthInch=dm.widthPixels;
    int heightInch=dm.heightPixels;
    int dens=dm.densityDpi;
    double wi=(double)widthInch/(double)dens;
    double hi=(double)heightInch/(double)dens;
    double x = Math.pow(wi,2);
    double y = Math.pow(hi,2);
    double screenInches = Math.sqrt(x+y);
    screenInches = (double)Math.round(screenInches * 10) / 10;

    int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));

    Toast.makeText(getApplicationContext(), width+"*"+height+" - "+widthPix+
            " - " +screenInches,
            Toast.LENGTH_LONG).show();

  //================================================================
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //get density per inch for example: 120 , 160 , 240
    float mXDpi = metrics.xdpi; // 160 The exact physical pixels per inch of the screen in the X dimension. 
    float mYDpi = metrics.ydpi;

    //density
    int nDensity = metrics.densityDpi; // 160 screen density expressed as dots-per-inch

          float  mMetersToPixelsX = mXDpi / 0.0254f; // 1 inch == 0.0254 metre
          float  mMetersToPixelsY = mYDpi / 0.0254f;

    //Resolution
    //The total number of physical pixels on a screen.
    int wPix = metrics.widthPixels; // 320 The absolute width of the display in pixels.
    int hPix = metrics.heightPixels; // 480 The absolute height of the display in pixels.
    int nWidthDisplay = (wPix < hPix)? wPix : hPix;

    float nWidthScreenInInch = wPix / mXDpi; //320 / 160 == 2.0 in inch.
    float nHeightScreenInInch = hPix / mYDpi; //480 / 160 == 3.0 in inch.

    double screenInches = Math.sqrt( nHeightScreenInInch*nHeightScreenInInch + 
            nWidthScreenInInch * nWidthScreenInInch); 

    screenInches = (double)Math.round(screenInches * 10) / 10;

    Toast.makeText(getApplicationContext(),"Screen size: "+screenInches,
            Toast.LENGTH_LONG).show();

    //================================================================
    DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    float hgt=metrics.heightPixels/metrics.xdpi;
    float wdth=metrics.widthPixels/metrics.ydpi;
    double screenInches = FloatMath.sqrt(hgt*hgt+wdth*wdth);
    screenInches = (double)Math.round(screenInches * 10) / 10;

    Toast.makeText(getApplicationContext(),width+"*"+height+" - "+screenInches,
                Toast.LENGTH_LONG).show();
    //===============================================================

How to get android device screen size?

Calculating android screen size?

Detect 7 inch and 10 inch tablet programmatically

Community
  • 1
  • 1

1 Answers1

0
DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    final int height = metrics.heightPixels;
    int width = metrics.widthPixels;