Ambran
Ambran

Reputation: 2627

Android: Two different layout includes in a RelativeLayout

I have a bit of a long layout file which i want to restructure by pulling two chunks out of the layout file into independent layout files, and include them in the main layout file.

My problem is how to make include2 come right underneath include1 as in normal RelativeLayout behaviour. The include tag doesn't know layout_above and layout_below. A solution would be using a LinearLayout instead, but then the button attached to the bottom of the screen (in the main layout file) cannot hold its place any more.

Main layout file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.stats.weekly.WeeklyStatsFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/include1"
            layout="@layout/weekly_stats_header_view" />

        <!-- Here I can't tell include2 to position itself under include1 -->
        <include
            android:id="@+id/include2"
            layout="@layout/weekly_stats_content_view" />

        <LinearLayout
            android:id="@+id/statsButtonContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/statsButton"
                style="@style/StandardButtonGreen"
                android:layout_marginBottom="@dimen/material_horizontal_padding_half"
                android:layout_marginLeft="@dimen/material_horizontal_padding_half"
                android:layout_marginRight="@dimen/material_horizontal_padding_half"
                android:layout_marginTop="@dimen/material_horizontal_padding_half"
                android:elevation="2dp"
                android:text="@string/strWeeklyStatsActivityButtonText" />

        </LinearLayout>

    </RelativeLayout>

</FrameLayout>

Solution update:

As suggested by Chirag and Dipali, I added layout_width and layout_height so the Layout_above and layout_below become relevant:

...
<include
    android:id="@+id/include2"
    layout="@layout/weekly_stats_content_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@+id/weekly_stats_header_view"
    android:layout_above="@+id/statsButtonContainer"/>
...

Upvotes: 0

Views: 84

Answers (4)

pooja
pooja

Reputation: 393

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/include1"
        layout="@layout/demo1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <include
        android:id="@+id/include2"
        layout="@layout/demo2"
        android:layout_below="@+id/include1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

    <LinearLayout
        android:id="@+id/statsButtonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

       <Button
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="Button"/>

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Dipali Shah
Dipali Shah

Reputation: 3798

Use your xml Just add layout_width and layout_height with layout_below property in include2.

    <include
                android:id="@+id/include2"
                layout_below = "@+id/include1"
                layout_width = "match_parent"
                layout_height = wrap_content"
                layout="@layout/weekly_stats_content_view" />

Upvotes: 1

Chirag Arora
Chirag Arora

Reputation: 816

use like that with Relative Layout and if you want to use with Linear Layout then use weight :

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/include1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        layout="@layout/weekly_stats_header_view" />

    <!-- Here I can't tell include2 to position itself under include1 -->
    <include
        android:id="@+id/include2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/include1"
        layout="@layout/weekly_stats_content_view" />

    <LinearLayout
        android:id="@+id/statsButtonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/include2">

        <Button
            android:id="@+id/statsButton"
            android:elevation="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test" />

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Oleksandr Zaiats
Oleksandr Zaiats

Reputation: 858

Just use LinearLayout with includes and a button inside RelativeLayout. Or 2 LinearLayouts - one with includes, another with button

Upvotes: 0

Related Questions