47

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?

7 Answers 7

101

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.

5
  • But the other ones have a value, and I want to keep that value, not set them to 0.
    – Xander
    Mar 21, 2013 at 19:18
  • 20
    If you're concerned about losing existing padding you can pass in textView.getPaddingTop() (or bottom,left,right) for the values to preserve
    – dymmeh
    Mar 21, 2013 at 19:18
  • @Merlin so just re-use the existing value for other sides padding by using getPaddingTop(), getPaddingLeft() and so on as already said above
    – Droidman
    Mar 21, 2013 at 19:20
  • I'm Using the same code like childTV.setPadding(0, 0, 0, 0); but it do not set the padding to 0. Base on some condition I'm trying to remove padding add in added in xml file. Feb 10, 2018 at 11:00
  • According to the setPadding docs: "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." So using getPadding isn't reliable in all cases. Without knowing the exact rules (which aren't given), I would feel uncomfortable using this technique.
    – big_m
    Dec 7, 2019 at 14:06
23

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)
2

Kotlin Extension Solution

You can add an extension variable like this for each of the specific sides.

inline var View.topPadding: Int
    get() = paddingTop
    set(@Px value) = setPadding(paddingLeft, value, paddingRight, paddingBottom)

// Then call
myView.topPadding = 10 // px

If you want to use dp instead of px, add something like this:

inline var View.rightPaddingDp: Float
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingRight.toFloat(), resources.displayMetrics) 
    set(value) {
        val rightPx = resources.displayMetrics.density * value
        setPadding(paddingLeft, paddingTop, rightPx.toInt(), paddingBottom)
    }

myView.rightPaddingDp = 10 // dp

To handle horizontal or vertical, do something like this. Note that the getter result wouldn't make sense, so you can disable it.

inline var View.horizontalPadding: Int
    get() = throw UnsupportedOperationException("No getter for property")
    set(@Px value) = setPadding(value, paddingTop, value, paddingBottom)

To use start or end and correctly handle RTL languages, you'll need to add this:

inline val View.isLtr get() = SDK_INT < 17 || layoutDirection == View.LAYOUT_DIRECTION_LTR

inline var View.startPadding: Int
    get() = if (isLtr) paddingLeft else paddingRight
    set(@Px value) {
        val left = if (isLtr) value else paddingLeft
        val right = if (isLtr) paddingRight else value
        setPadding(left, paddingTop, right, paddingBottom)
    }

Bonus: Make an equivalent that takes a res. i.e. topPaddingRes

0

You can also use this

setPadding(view, 500, Padding.TOP);

with help of a @IntDef definition:

public static void setPadding(View view, int padding, @Padding.Direction int direction) {
    switch (direction) {
        case Padding.LEFT:
            view.setPadding(padding, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
            return;
        case Padding.RIGHT:
            view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), padding, view.getPaddingBottom());
            return;
        case Padding.TOP:
            view.setPadding(view.getPaddingLeft(), padding, view.getPaddingRight(), view.getPaddingBottom());
            return;
        case Padding.BOTTOM:
            view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), padding);
            return;
        default:
    }
}

public static class Padding {
    @IntDef({Padding.LEFT, Padding.RIGHT, Padding.TOP, Padding.BOTTOM})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Direction {}
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int TOP = 2;
    public static final int BOTTOM = 3;
}
0

Below code is working fine.

float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
0

A kotlin View extension to set any combination of the edges to the same value at one go for any View type:

enum class Edge{
    TOP,
    LEFT,
    BOTTOM,
    RIGHT
}
fun View.padding(edges:Set<Edge>, value:Int){
    var top    = paddingTop
    var left   = paddingLeft
    var bottom = paddingBottom
    var right  = paddingRight
    for(edge in edges){
        when(edge){
            Edge.TOP    -> top    = value
            Edge.LEFT   -> left   = value
            Edge.BOTTOM -> bottom = value
            Edge.RIGHT  -> right  = value
        }
    }
    setPadding(left, top, right, bottom)
}

Example:

someView.padding(setOf(Edge.RIGHT, Edge.LEFT), 50)
0

Use updatePadding:

val top = resources.getDimensionPixelOffset(R.dimen.padding)
textView.updatePadding(top = value) // value in pixels

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.