Search code examples
androidandroid-fragmentsandroid-constraintlayoutandroid-framelayout

Frame layout is not positioned properly inside ConstraintLayout


I've placed a FrameLayout inside ConstraintLayout and I think it's constrained properly. But in the app, it's not positioned properly. FrameLayout is replaced by appropriate fragment at runtime.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.chapters.ChapterActivity">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_chapter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="Chapters"
        app:titleTextColor="@color/White" />

    <FrameLayout
        android:id="@+id/chapter_fragment_container_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="500dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.388"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_chapter"
        app:layout_constraintVertical_bias="1.0" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/White"
        app:itemTextColor="@color/Wheat"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/chapter_bottom_menu">

    </com.google.android.material.bottomnavigation.BottomNavigationView>


</androidx.constraintlayout.widget.ConstraintLayout>

Here's what is shown in IDE:

IDE Design Preview

But this is what I get in device:

App run in Device Preview

FrameLayout should be placed in-between toolbar & the bottom navigation bar, it should be done by LinearLayout/Relative one, haven't tried though.. but what's wrong with this.


Solution

  • Change this FrameLayout part as below:

     <FrameLayout
            android:id="@+id/chapter_fragment_container_frameLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:minHeight="500dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.388"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar_chapter"
            app:layout_constraintVertical_bias="1.0" />
    

    The reason why it is happening is because of match_constraint. If you have defined constraint for top, bottom, start, and end you should define 0dp for height and width to View.