I want to set the top padding of a textview programmatically. I know you can do this with the method setPadding()
. But the problem is that this method requires 4 parameters: left, top, right, bottom. I don't want to change the left, right and bottom, I just want to change the top padding.
Is that possible?
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)
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