Set margins in a LinearLayout programmatically
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To set margins programmatically in a LinearLayout, you need to work with the child view’s layout params, not the parent layout itself. In Android, margins live on ViewGroup.MarginLayoutParams, and for a LinearLayout child that usually means LinearLayout.LayoutParams. The most common bug is setting the wrong layout param type or forgetting to convert density-independent units into pixels.
Create the Right LayoutParams
When you add a view to a LinearLayout in code, create LinearLayout.LayoutParams and call setMargins.
Kotlin example:
This works because the child view is receiving the layout params type expected by its LinearLayout parent.
Convert dp to Pixels
Margin values in Android layout XML are usually expressed in dp, but setMargins expects raw pixels. If you hardcode 16 without conversion, the visual result varies by screen density.
Use a helper:
This is the correct way to make programmatic margins visually match your XML-based layout expectations.
Update Margins on an Existing View
If the view already exists, read its current layout params, cast them, and then update the margins:
Reassigning layoutParams helps ensure the view is remeasured and redrawn with the updated values.
If you are writing generic utility code, you can also work against ViewGroup.MarginLayoutParams when the parent type is not fixed up front. The key idea stays the same: margins belong to the child’s layout params object, not to the LinearLayout itself.
Use Start and End Margins for Direction-Aware Layouts
If your app supports right-to-left layouts, marginStart and marginEnd can be more appropriate than left and right margins.
This respects layout direction better than hardcoding left and right values.
Margins and Existing XML Values
If a view already has XML margins, your programmatic update replaces those runtime values. That can be useful, but it also means you should update all four sides deliberately instead of assuming the old values will remain untouched. Reading the existing params first and then modifying only the sides you want is often the safest pattern.
Java Version
If you are working in Java instead of Kotlin, the same idea applies:
The underlying API is the same. Only the syntax changes.
Common Pitfalls
One common mistake is casting to the wrong layout params type. If the parent is a LinearLayout, the child should use LinearLayout.LayoutParams, not RelativeLayout.LayoutParams or another unrelated class.
Another mistake is treating raw integers as dp. setMargins expects pixels, so density conversion matters.
Developers also sometimes update the margins on a temporary params object and forget to put it back on the view. Reassigning the updated params is a safer pattern.
Finally, remember that margins belong to the child view’s layout params. Padding is different; padding is space inside the view, not outside it.
Summary
- Set margins programmatically by editing the child view’s
LinearLayout.LayoutParams. - Convert
dpvalues to pixels before passing them tosetMargins. - Cast existing layout params before updating margins on a view already in the layout.
- Use start and end margins when layout direction matters.
- Do not confuse margins with padding or with the parent layout’s own spacing.

