2

I'm trying to set fix width to a view. “in”, “mm”, and “pt” are density independent and the same size on every device or i am wrong? My view width should be 141.3pt/49.8mm , so i am setting in the XML android:layout_width="141.3pt" or via code:

float requiredPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, (float) 141.3, dm);
ViewGroup.LayoutParams params = mRequiredSizeLine.getLayoutParams();
        params.width = (int)requiredPx;
        mRequiredSizeLine.setLayoutParams(params);

The thing is that, this is ok on some devices(on most of them) but for example on Samsung Galaxy Stratosphere 2 it is not ok. Also i have a method that calculate screen display width in in and mm

private double checkDeviceWidthInInches(){
    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);

    return Math.sqrt(x);

and you can notice from the first image below, this is also incorrect(it is not possible the screen width to be 76 mm) .

Image of Samsung Galaxy Stratosphere 2(THIS IS INCORRECT) and Image of Samsung Galaxy S4(THIS IS CORRECT)

So what do you think? Could be some problem with the device or ? Link for device spec http://www.samsung.com/us/mobile/cell-phones/SCH-I415SAAVZW

Any suggestion? Thanks

nikolaDev
  • 1,802
  • 2
  • 14
  • 15

1 Answers1

0

You should always specify dimensions of your views in dp (density-independent pixels). Avoid using other units. When you want to specify a size of text use sp units. Difference between px, dp, dip and sp in Android

Community
  • 1
  • 1
michal.z
  • 2,025
  • 1
  • 15
  • 10
  • That is not the solution. Please check my question again i made some changes. – nikolaDev Dec 07 '14 at 12:27
  • What is a reason you really want to stick with using pt/mm/in units? – michal.z Dec 07 '14 at 12:32
  • Well from the link that you provided it says that pt/in/mm are units of measurement that have same physical size on every screen and that is what i want to achieve. The width should be FIX on every device. But as you can see, the strange thing on that device, is that the method returns incorrect physical width size, so i am wondering if the device is the problem? – nikolaDev Dec 07 '14 at 12:43
  • 1
    It appears that some devices can give improper information about their dpi. Many people experienced that and wrote about it i.e. [Android devices and use of MM/INCH](http://grokbase.com/t/gg/android-developers/128w2687v9/android-devices-and-use-of-mm-inch). If so computing pixel size which corresponds to specified physical size basing on the wrong dpi will lead to improper results. Unfortunately I don't see any easy solution to that. And yes, probably device is a problem. – michal.z Dec 07 '14 at 13:06