I think my question should be simple to answer, but I really don't know the answer so here goes:
I am trying to subtract the original height from a layout
50 dps
. Right now I do it as follows:
v.getLayoutParams().height = initialHeight - 50;
v.requestLayout();
The problem with this is that it's subtracting via the metric px
, and I'd like to work withdp
You can convert from dp to pixels as follows:
Original source in the official documentation available in Spanish.
Since the answer so far didn't seem helpful, just so you understand:
Can't work with dp in
LayoutParams
. Internally, the classes work with pixels. So the only way to work with values in dp is to convert them to pixels and then apply the pixels.Another alternative is to define the dp unit in the dimens.xml file
and in your code put:
font
The correct way is described in the Android source code :
by Romain Guy who was working with Android at its inception.
This is also described in the documentation: Converting dp units to pixel units.
Method to convert dp to pixels.
You should convert the 50dp you want to pixels and then subtract these pixels from the height of the view. The number of pixels will be different depending on the pixel density of the device.
This can help you, it explains very well how dp works, it helped me a lot:
What are DPs in Android and what are they for?