Use the following code to get pixels from a given value in dp
.
Resources res = getResources(); float value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDP, res.getDisplayMetrics());
where valueInDP is the value in dp, and this will return the corresponding pixel value in accordance with the density of the screen.
or you can use the following -
float value = valueInDP * getResources().getDisplayMetrics().density;
source share