Android: set just one padding of textview programmatically
use
yourTextView.setPadding(0, 10, 0, 0);
Adjust only the parameters you need and set the other ones to zero.
If you need to preserve other existing paddings, use yourView.getPaddingLeft()
, yourView.getPaddingTop()
and so on.
I usually create a simple utility method just to not forget, or misplace the other paddings:
public static void setPaddingLeft(View v, int leftPaddingDp) {
int leftPaddingPx = dpToPx(leftPaddingDp);
v.setPadding(leftPaddingPx, v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());
}
To be used later like this, supplying dp units, as if would in xmls:
Utils.setPaddingLeft(myExampleTextView, 10)