ConstraintLayout change constraints programmatically
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The standard way to change ConstraintLayout constraints at runtime is to use ConstraintSet. It lets you clone the current layout, change the relationships between views, and then apply the new constraints back to the layout without rebuilding the whole screen.
Why ConstraintSet Is the Right Tool
Each child view inside a ConstraintLayout has constraint-related layout parameters, but editing those parameters directly becomes hard to read once the relationships get more complex. ConstraintSet gives you a more declarative way to update the layout.
The normal sequence is:
- get the
ConstraintLayout - clone its current constraints into a
ConstraintSet - clear or reconnect the constraints you want to change
- apply the set back to the layout
That is easier to maintain than manually editing every margin and anchor field on LayoutParams.
Basic Example in Kotlin
Suppose a button starts aligned under one view and later needs to move under another view:
clear removes the old anchor. connect creates the new one. applyTo tells the layout to re-run with the new constraints.
XML IDs Still Matter
Programmatic constraint changes work best when the views already have stable IDs in XML:
Without stable IDs, you have nothing reliable to connect against.
Animating the Constraint Change
A direct applyTo works, but it can look abrupt. If you want a smoother UI transition, wrap the change with TransitionManager:
That is often enough for simple position or size changes. For more advanced motion, MotionLayout may be a better fit, but ConstraintSet plus a delayed transition handles many day-to-day cases cleanly.
Creating Alternative Layout States
If the same constraint configuration is reused, it can be cleaner to maintain multiple states rather than building every rule from scratch in code.
This pattern works well for simple UI state machines such as collapsed versus expanded content, login versus signed-in layouts, or compact versus detailed cards.
When Direct LayoutParams Editing Is Enough
For tiny one-off changes, direct layout parameter edits can work, but only if you are disciplined about casting and reassigning correctly. In most real screens, ConstraintSet stays clearer because it mirrors the mental model of "connect this view to that anchor".
Common Pitfalls
The most common mistake is adding a new constraint without clearing the old conflicting one. That can leave the view over-constrained or positioned in an unexpected place.
Another issue is forgetting to call applyTo, which means all your edits stay in the ConstraintSet object and never affect the screen. Developers also often forget IDs on views created dynamically, making runtime connection logic much harder. Finally, if you expect smooth animation and do not use a transition, the layout jump can look like a bug even when the constraints are correct.
Summary
- Use
ConstraintSetto changeConstraintLayoutrelationships at runtime. - Clone the current layout, edit constraints, then call
applyTo. - Clear conflicting constraints before connecting new ones.
- Keep stable view IDs so constraints can be targeted reliably.
- Add
TransitionManager.beginDelayedTransitionif you want the changes to animate smoothly.

