Changing Relative Layout Prarams from the Code

Relative layout is the best when it comes to alignment of the child objects in it. The properties like centre_in_parent , centre-horizontal/vertical help you easily align views in a relative layout.

But, in some cases you’ll need to do the dynamically meaning from the code. To do that Relative Layout has a special object called RelativeLayout.LayoutParams. We can use this to dynamically add rules and params to the Relative Layout.

Let’s see how to do it in action :

<RelativeLayout 
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"/>
</RelativeLayout>

Here we have two TextViews in a RelativeLayout. We perform alignments with the texts to show you how this works. Firstly reference all the required views including the relative layout in your Activity.java file.

 relativeLayout = findViewById(R.id.root);
 text = findViewById(R.id.text);
 text2 = findViewById(R.id.text2);

Now that we have the reference to the views in the layout, Create a RelativeLayout.LayoutParams object and cast it to the text param in your layout file.

RelativeLayout.LayoutParams textParams = (RelativeLayout.LayoutParams) text.getLayoutParams();

If you’re using android studio, you can easily cast the text.getLayoutParams() to Relative.LayoutParams using the suggestion by the IDE.

Android Studio Suggestion

Just select the first suggestion and you’re done.


Playing around with the RULES

Rules are basically the attributes that you specify in the layout file. The RelativeLayout.LayoutParams class provides a method to add rules from the code.

 // params without subject
 textParams.addRule(RelativeLayout.CENTER_IN_PARENT);
 textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
 textParams.addRule(RelativeLayout.CENTER_VERTICAL);

These are equivalent to :

 android:layout_centerInParent="true"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"

in the layout file.

In case, you wanted to use the to_right_of or to_left_of which requires a subject to align with it, RelativeLayout.LayoutParams has a method for that too.

 // To add with a subject
textParams.addRule(RelativeLayout.START_OF, R.id.text2);
textParams.addRule(RelativeLayout.END_OF, R.id.text2);
textParams.addRule(RelativeLayout.RIGHT_OF, R.id.text2);
textParams.addRule(RelativeLayout.LEFT_OF, R.id.text2);

These are equivalent to :

android:layout_toStartOf="@id/text2"
android:layout_toEndOf="@id/text2"
android:layout_toRightOf="@id/text2"
android:layout_toLeftOf="@id/text2"

in your layout file.


What if I want to remove a rule ?

And yes, you can do that. RelativeLayout.LayoutParams has a method to remove rules that were applyed to the view. Remember this might remove a rule that you applied not only through code but also in your layout file.

// Remove params
textParams.removeRule(RelativeLayout.CENTER_IN_PARENT);
textParams.removeRule(RelativeLayout.START_OF);

And that’s how you remove any rule you applied to the RelativeLayout before.

If you have anykind of issues or if something isn’t clear in the blog, feel free to mention it in the comments section below and I’ll be happy to help you with it.