Android set padding programmatically in dp(dip)

final float scale = getResources().getDisplayMetrics().density;
int padding_5dp = (int) (5 * scale + 0.5f);
int padding_20dp = (int) (20 * scale + 0.5f);
int padding_50dp = (int) (50 * scale + 0.5f);

RadioButton rb = new RadioButton(this);
rb.setText("My Radio Button");
rb.setLayoutParams(new LayoutParams(padding_50dp,padding_50dp));
rb.setPadding(padding_20dp,padding_5dp,padding_5dp,padding_5dp);

public void setPadding (int left, int top, int right, int bottom)

Sets the padding. The view may add on the space required to display the scrollbars, depending on the style and visibility of the scrollbars. So the values returned from getPaddingLeft(), getPaddingTop(), getPaddingRight() and getPaddingBottom() may be different from the values set in this call.

Related XML Attributes

  • android:padding
  • android:paddingBottom
  • android:paddingLeft
  • android:paddingRight
  • android:paddingTop

Parameters
  • left the left padding in pixels
  • top the top padding in pixels
  • right the right padding in pixels
  • bottom the bottom padding in pixels

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.