Search code examples
androidandroid-layoutandroid-toolbar

How to fix this when Fragment is under the toolbar?


I need help with this xml layout because my Fragment is under the toolbar.
I have tried all kinds of layouts attributes like layout_below and more but no success. Also the NavigationView is under the toolbar.

The ConstraintLayout like app:layout_constraintTop_toTopOf has no effect if I make the container ConstraintLayout

enter image description here

Here´s my xml hoping for some advice?

<?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"
    xmlns:autofit="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.port.android.ui.ActivityMain">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.port.android.ui.ActivityMain">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/main_ConstraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <FrameLayout
                    android:id="@+id/frame"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                </FrameLayout>
            </LinearLayout>
        </android.support.constraint.ConstraintLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            app:headerLayout="@layout/navigation_view_header"
            app:menu="@menu/menu_navigation">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="horizontal"
                android:padding="20dp">

                <Button
                    android:id="@+id/btn_exit_address"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="exitAddress"
                    android:text="@string/exit_address" />
            </LinearLayout>

        </android.support.design.widget.NavigationView>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Solution

  • Try layout below in your drawer layout

    <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context="com.port.android.ui.ActivityMain"
            android:layout_below="@+id/appBarLayout_1">