Search code examples
androidandroid-relativelayout

Relative layout constraints


I was trying to create layout but don't understand how to put one view under another. Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:id="@+id/scroll_to_continue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardview_dark_background"
            android:gravity="center"
            android:text="Scroll to continue with content"
            android:textColor="@color/colorAccent"
            android:textSize="18sp"
            android:typeface="sans" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_above="@id/scroll_to_continue">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/design_default_color_primary"
                android:gravity="center"
                android:text="Tap to Learn More"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                android:typeface="sans" />
        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

And I want to put LinearLayout right ABOVE TextView with id scroll_to_continue. Can somebody help me with that?

P.S. android:layout_above="@id/scroll_to_continue" doesn't work as expected as well as addRule(RelativeLayout.ABOVE, scrollToContinue.getId());


Solution

  • First of all you have provide TextView's position in the layout. I have set it to bottom using android:layout_alignParentBottom="true". Then it's work. Check below:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">
    
            <TextView
                android:id="@+id/scroll_to_continue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/cardview_dark_background"
                android:gravity="center"
                android:text="Scroll to continue with content"
                android:layout_alignParentBottom="true"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                android:typeface="sans" />
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_above="@id/scroll_to_continue">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/design_default_color_primary"
                    android:gravity="center"
                    android:text="Tap to Learn More"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:typeface="sans" />
            </LinearLayout>
    
        </RelativeLayout>
    
    </FrameLayout>