Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View.setPadding accepts only in px, is there anyway to setPadding in dp?

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?

like image 389
Sharj Avatar asked Nov 25 '10 10:11

Sharj


People also ask

How do you add padding to a view?

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).


1 Answers

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); 
like image 53
Labeeb Panampullan Avatar answered Oct 12 '22 16:10

Labeeb Panampullan