Devendra Dagur
Devendra Dagur

Reputation: 850

Include a new layout, inside CoordinatorLayout not working properly?

I have two layouts. First layout file name is home.xml and second name is coordinator.xml. So I just want to include home.xml in coordinator.xml. But the problem is when I try to include this layout in coordinator.xml, it cross mobile screen and go inside of it.

If I include app:layout_behavior="@string/appbar_scrolling_view_behavior" in my include layout it cross mobile screen and go inside of it.

If I don't include this behaviour it overlap the Toolbar.

So what is the solution ?

home.xml codes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:id="@+id/view"
        android:layout_above="@+id/linear1"
        android:background="#00409f"
        android:layout_marginBottom="10dp"
        />

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

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/home"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Home"/>

        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/wallet"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Wallet"/>

        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/qr_code"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="QR Code"/>

        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/user_male"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Profile"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

coordinator.xml codes:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".Home">


    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:contentInsetStart="0dp"
            android:contentInsetLeft="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay">
            <include layout="@layout/actionbar_layout"/>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>


    <!--Include layout here give me problem-->

    <include layout="@layout/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        />

</android.support.design.widget.CoordinatorLayout>

With app:layout_behavior="@string/appbar_scrolling_view_behavior" it look like this:

problem image

Without app:layout_behavior="@string/appbar_scrolling_view_behavior" it look like this:

problem img

Upvotes: 2

Views: 6226

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Include the layout like this:

<include layout="@layout/home" />

And just use that app:layout_behavior="@string/appbar_scrolling_view_behavior" in the layout you want to be under the AppbarLayout so:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/linear1"
        android:layout_marginBottom="10dp"
        android:background="#00409f" />

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Home"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Wallet"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="QR Code"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Profile"
                android:textColor="#000000" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

UPDATE: I just rechecked again and here is the another layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:fitsSystemWindows="true"
    tools:context=".Home">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>


    <include layout="@layout/home" />

</android.support.design.widget.CoordinatorLayout>

After compiled:

enter image description here

For that overlaping, we need to see the actionbar_layout but for now, the above layout should work.

Upvotes: 7

Related Questions