Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 750
Tord Larsen Avatar asked Feb 23 '17 19:02

Tord Larsen


2 Answers

For those like me that will be, eventually, looking for an answer for that problem.

The way I fixed that was using a <ConstraintLayout> and changing the child <FrameLayout> layout_height and layout_width to "0dp" which is equals to "MATCH_CONSTRAINT". Additionally, you should also add some constraint attributes like below.

Your <FrameLayout>should look like this:

<FrameLayout
        android:id="@+id/fragment_placeholder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/main_toolbar"
        app:layout_constraintBottom_toTopOf="@id/navigation"
/>

I hope it helps!

like image 193
Gui Herzog Avatar answered Oct 12 '22 21:10

Gui Herzog


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">
like image 40
Aman Avatar answered Oct 12 '22 21:10

Aman