Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: set just one padding of textview programmatically

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?

like image 847
Xander Avatar asked Mar 21 '13 19:03

Xander


2 Answers

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.

like image 109
Droidman Avatar answered Oct 09 '22 04:10

Droidman


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) 
like image 22
pkk Avatar answered Oct 09 '22 03:10

pkk