How to set margin of ImageView using code, not xml
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Setting margins in Android code is mostly about choosing the correct layout parameters for the view's parent. An ImageView does not own its own margin model, so you update the margin fields on the LayoutParams instance attached to that view.
How Android Margins Work
Margins are defined by ViewGroup.MarginLayoutParams, and most common parent layouts expose a subclass of that type. That detail matters because the object returned by imageView.layoutParams must match the container that holds the view. If the parent is a LinearLayout, use LinearLayout.LayoutParams. If the parent is a ConstraintLayout, use ConstraintLayout.LayoutParams.
A reliable pattern is:
- Create or obtain the
ImageView. - Read its current layout params.
- Cast them to a margin-capable type.
- Set the margins in pixels.
- Assign the params back or call
requestLayout().
If you are adding the view entirely in code, you can build the params up front.
Setting Margins In Kotlin
The following example adds an ImageView to a LinearLayout and sets a start and top margin in density-independent units.
The setMargins call expects pixel values. Converting from dp keeps spacing visually consistent across devices.
Updating An Existing ImageView
If the view already exists in the hierarchy, retrieve the current params and update them instead of creating a new ImageView. This is common when reacting to state changes.
Using ViewGroup.MarginLayoutParams is often enough when you only need margins, but you still need a parent layout that actually produced a compatible subclass.
Java Example
If your project is still using Java, the same rule applies.
The important part is not the language. It is the type of layout params and the conversion from dp to pixels.
Parent-Specific Cases
Some layouts add extra positioning rules on top of margin support.
For ConstraintLayout, margins apply only after you define constraints. An image with margins but no constraints may still appear in an unexpected position.
For FrameLayout, margins work, but there is no sibling flow like in LinearLayout. You usually combine margins with gravity if you want the image anchored to a corner.
Common Pitfalls
Using the wrong LayoutParams type is the most common failure. Casting to LinearLayout.LayoutParams when the parent is actually a ConstraintLayout throws ClassCastException. Inspect the parent or use ViewGroup.MarginLayoutParams when appropriate.
Another issue is passing raw integers and assuming they mean dp. Android interprets those values as pixels, so spacing looks tiny on high-density screens. Always convert display units with TypedValue.applyDimension or a utility extension.
It is also easy to forget that margins belong to the child within the parent. Padding affects the inside of the ImageView, not the distance outside it. If the goal is to create space between the image and surrounding views, use margins, not padding.
Finally, changing params without reassigning them can fail on some code paths. Updating the object and then setting imageView.layoutParams = params makes the intent explicit and avoids stale layout behavior.
Summary
- Margins on an
ImageVieware controlled by the view'sLayoutParams, not by theImageViewitself. - Use a params type that matches the parent layout, such as
LinearLayout.LayoutParamsorConstraintLayout.LayoutParams. - Convert
dpvalues to pixels before callingsetMarginsor settingtopMargin,marginStart, and related fields. - For existing views, update the current params and call
requestLayout()after reassignment. - Distinguish margins from padding so spacing changes affect the correct part of the layout.

