Android function View.setPadding(int left, int top, int right, int bottom)
only accepts values in px but I want to set padding in dp. Is there any way around it?
Use the setPadding(left, top, right, bottom) method on the view to set the padding. The integers passed as parameters to this function are the number of pixels (px), which are different from the density-independent pixels (dp or dip).
Straight to code
int padding_in_dp = 6; // 6 dps final float scale = getResources().getDisplayMetrics().density; int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With