How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android development, you often need to position views dynamically at runtime rather than declaring everything in XML. One common scenario is aligning a Button to the right edge of its parent RelativeLayout. While XML makes this simple with the android:layout_alignParentRight="true" attribute, doing the same thing programmatically requires working with RelativeLayout.LayoutParams and its addRule() method. This article walks through the process step by step, with examples in both Java and Kotlin.
How RelativeLayout Positioning Works
A RelativeLayout arranges child views based on rules that describe their position relative to the parent container or to sibling views. These rules are stored in a RelativeLayout.LayoutParams object attached to each child view. When you set layout_alignParentRight in XML, Android internally creates a layout rule for that view. Programmatic positioning does the same thing, just through code instead of markup.
The key rules relevant to parent alignment include ALIGN_PARENT_RIGHT, ALIGN_PARENT_LEFT, ALIGN_PARENT_TOP, ALIGN_PARENT_BOTTOM, and CENTER_IN_PARENT. Each rule is an integer constant on the RelativeLayout class.
Setting layout_alignParentRight in Java
Here is the complete process in Java.
The addRule(RelativeLayout.ALIGN_PARENT_RIGHT) call tells the layout system to pin the right edge of the Button to the right edge of the parent. You can stack multiple rules on the same LayoutParams object. For example, to align both right and vertically centered, add both rules.
Setting layout_alignParentRight in Kotlin
The Kotlin version is nearly identical, just with more concise syntax.
Removing a Rule
If you need to remove the alignment later (for example, in response to a user action), call addRule with a value of 0 on the same verb.
After changing layout params on an already-attached view, call requestLayout() on the parent to trigger a re-layout pass.
RTL Support with ALIGN_PARENT_END
Starting from API level 17, Android introduced ALIGN_PARENT_START and ALIGN_PARENT_END as alternatives to ALIGN_PARENT_LEFT and ALIGN_PARENT_RIGHT. These are layout-direction-aware, meaning ALIGN_PARENT_END maps to the right side in left-to-right (LTR) locales and to the left side in right-to-left (RTL) locales like Arabic or Hebrew.
Using ALIGN_PARENT_END instead of ALIGN_PARENT_RIGHT is the recommended practice for apps that support multiple locales.
Common Pitfalls
- Forgetting to call setLayoutParams(). Creating a
LayoutParamsobject and adding rules is not enough. You must assign it to the view withsetLayoutParams()(or thelayoutParamsproperty in Kotlin). Without this step, the rules are never applied. - Conflicting rules. Adding
ALIGN_PARENT_RIGHTandALIGN_PARENT_LEFTon the same view stretches it across the full width of the parent. This is sometimes intentional, but if you only wanted right alignment, make sure to remove the left rule first. - Not triggering a re-layout. When modifying params on a view that is already part of the view hierarchy, call
requestLayout()so the layout system recalculates positions. - Ignoring RTL locales. Using
ALIGN_PARENT_RIGHTworks fine for LTR layouts but breaks the experience for RTL users. PreferALIGN_PARENT_ENDfor production apps. - Using ConstraintLayout instead. For new projects, Google recommends
ConstraintLayoutoverRelativeLayoutbecause it handles complex layouts with a flat hierarchy and better performance. If you are starting fresh, consider usingConstraintLayoutconstraints instead.
Summary
To programmatically align a Button to the right of its parent RelativeLayout, create a RelativeLayout.LayoutParams object, call addRule(RelativeLayout.ALIGN_PARENT_RIGHT), and assign the params to the view. Use ALIGN_PARENT_END for RTL-aware positioning. Remember to trigger a layout pass with requestLayout() when modifying an already-displayed view. While RelativeLayout remains widely used in existing codebases, consider ConstraintLayout for new development.

