'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?



Solution 1:[1]

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.

Solution 2:[2]

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)

Solution 3:[3]

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

Solution 4:[4]

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;
}

Solution 5:[5]

Below code is working fine.

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

Solution 6:[6]

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)

Solution 7:[7]

Use updatePadding:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alberto M
Solution 3
Solution 4
Solution 5 murugan mani
Solution 6
Solution 7 CoolMind