Android: Why does getDimension and getDimensionPixelSize both return the same?

user3591115 picture user3591115 · Jul 2, 2014 · Viewed 15.4k times · Source

I find the methods getDimension and getDimensionPixelSize confusing.

It would look to me as though getDimension would return the actual value stored in the dimension, for example 10dp and getDimensionPixelSize would return it converted to px.

But both seem to do the same thing...

Answer

laalto picture laalto · Jul 2, 2014

getDimension() returns a floating point number which is the dimen value adjusted with current display metrics:

getDimensionPixelSize() returns an integer. It is the same as getDimension() rounded to an int with any non-zero dimension ensured to be at least one pixel in size.

For integer dimension values they both return the same numeric value.