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):
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:
This keeps programmatic layouts consistent with dimensions defined in XML.
setPaddingRelative for RTL layouts
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:
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:
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
setPaddingwhere 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.
Related reading
- Add placeholder text inside UITextView in Swift?
- Add rounded corners to all UIImageViews
- add Shadow on UIView using swift 3
- Add swipe to delete UITableViewCell
- Add UIPickerView a Button in Action sheet - How?
- Add views in UIStackView programmatically
- Adding a simple UIAlertView
- Adding a UILabel to a UIToolbar
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.