Sharj Sharj - 1 year ago 101
Android Question

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


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?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download