Android Development
Programming
User Interface
Padding
Mobile App Design

Set padding on an Android view programmatically

Codemia Knowledge Hub ·

Call setPadding(left, top, right, bottom) on the view. The values are in pixels, so convert your dp values first, and use setPaddingRelative(start, top, end, bottom) when the layout should respect right-to-left languages.

setPadding takes pixels

Every View has setPadding(left, top, right, bottom):

kotlin
val textView = TextView(this)
textView.text = "Hello"
textView.setPadding(16, 16, 16, 16)

This works, but those 16s are pixels, not density-independent units, so the padding looks too small on high-density screens and inconsistent across devices.

Converting dp to pixels

Padding is usually designed in dp, so convert before calling setPadding:

kotlin
1import android.content.Context
2import android.util.TypedValue
3
4fun Context.dp(value: Int): Int {
5    return TypedValue.applyDimension(
6        TypedValue.COMPLEX_UNIT_DIP,
7        value.toFloat(),
8        resources.displayMetrics
9    ).toInt()
10}
11
12val card = View(this)
13card.setPadding(dp(16), dp(12), dp(16), dp(12))

This keeps programmatic layouts consistent with dimensions defined in XML.

setPaddingRelative for RTL layouts

kotlin
val messageView = TextView(this)
messageView.text = "RTL aware padding"
messageView.setPaddingRelative(dp(20), dp(12), dp(20), dp(12))

Start and end follow the layout direction automatically, which makes this the better default for modern Android apps. It matters most for directional content such as icons near the leading edge: a layout that looks right in English can feel subtly wrong in Arabic or Hebrew if the code hardcodes left and right where start and end were intended.

Changing one side and keeping the others

setPadding replaces all four sides at once. To change one edge, read the current values and reapply the full set:

kotlin
1val button = Button(this)
2
3button.setPadding(
4    button.paddingLeft,
5    button.paddingTop + dp(8),
6    button.paddingRight,
7    button.paddingBottom
8)

If the view uses relative padding, use paddingStart and paddingEnd with setPaddingRelative.

In custom components, check whether the view already gets padding from XML or a style. A blanket setPadding call can silently discard defaults that were there for readability or touch comfort.

Padding vs margin

Padding is space inside the view, between its content and its edges. Margin is space outside the view and belongs to the layout parameters. To space sibling views apart, set a margin:

kotlin
1val params = LinearLayout.LayoutParams(
2    LinearLayout.LayoutParams.MATCH_PARENT,
3    LinearLayout.LayoutParams.WRAP_CONTENT
4)
5params.topMargin = dp(12)
6
7textView.layoutParams = params

The two also affect backgrounds and click areas differently. More padding usually enlarges the content inset while keeping the view bounds the parent assigned. A margin changes how much space the parent reserves around the view.

Mistakes to avoid

  • Passing raw pixel values and getting inconsistent padding across screen densities.
  • Using setPadding where the layout needs start and end padding for RTL support.
  • Overwriting existing padding when you only meant to adjust one side.
  • Using padding to fix spacing between views, which is a margin problem.
  • Forgetting that padding changes the content area, which affects background drawables and touch targets.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms