Search code examples
androidandroid-relativelayout

set layout_below of RelativeLayout within a Relative Layout programmatically


I have a RelativeLayout @+id/main_layout.
Inside the @+id/main_layout, I have a TextView @+id/txt1 and a RelativeLayout @+id/store.
For some reason I have to set android:layout_below="@+id/txt1" to the inner RelativeLayout @+id/store programmatically. How to do that?


Solution

  • Can you try adding it like this:

    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
    

    Of course, use parameters (fill parent etc.) which you need!

    Edit: Of course, you don't need to create the RelativeLayout, just instantiate your own, and apply the rules you need!