Search code examples
androidandroid-coordinatorlayoutandroid-framelayout

FrameLayout overflows Window in CoordinatorLayout


I wanted to implement FrameLayout with toolbar for scrolling_view_behaviour. But when I set layout_height of FrameLayout to match_parent, it overflows as shown in the picture below:

enter image description here

this is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".ui.NewsActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        >

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/Widget.AppCompat.Toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:titleTextColor="#fff" />

    </com.google.android.material.appbar.AppBarLayout>


    <FrameLayout
        android:id="@+id/flFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_insetEdge="start">

        <fragment
            android:id="@+id/navHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"

            app:navGraph="@navigation/news_nav_graph" />

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="#fff"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:layout_insetEdge="bottom"
        app:menu="@menu/bottom_navigation_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I am sorry if this question is repeated or silly, I couldn't find anything which could solve my problem. Thanks in advance


Solution

  • You can remove attributes which refer to ConstraintLayout because you are not using one here. Also the FrameLayout. Adding margin to bottom of Fragment will set it in proper way.

    <androidx.coordinatorlayout.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">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/Widget.AppCompat.Toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:titleTextColor="#fff" />
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <fragment
            android:id="@+id/navHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="112dp"
            app:defaultNavHost="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:navGraph="@navigation/news_nav_graph"
            tools:ignore="FragmentTagUsage" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_gravity="bottom"
            android:background="#fff"
            app:menu="@menu/bottom_navigation_menu" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    As you can see i have added

    android:layout_marginBottom="112dp"
    

    But if you add 56dp to it, it will put bottom 56dp layout behind BottomNavigationView. To avoid that inside child fragments which are in other layouts just add more 56dp to it.