androidandroid-layoutandroid-constraintlayout

Is ConstraintLayout broken?


This is my Activity layout:

<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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

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

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbar"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:adUnitId="ca-app-pub-3940256099942544/9214589741"
        app:adSize="BANNER"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And this is a Fragment displayed inside it:

<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".ScannerFragment">

    <androidx.camera.view.PreviewView
        android:id="@+id/cameraPreview"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sale_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Total"
        android:textSize="24sp"/>

</androidx.appcompat.widget.LinearLayoutCompat>

And this is what it looks like:

enter image description here

Have a look at the FragmentContainerView: It is constraint to be below the appbar and above the adView. The fragment however clearly violates these constraints and ignores the appbar and the adView.

Am I doing something wrong? Or is ConstraintLayout simply broken? How to achieve the layout I want?

I already tried to put the FragmentContainerView inside a LinearLayout and the constraints on the LinearLayout, but could not witness any change.


Solution

  • Your FragmentContainer has a height of wrap_content meaning it won't respect the constraints set by constraintTop_toBottomOf (and similar) attributes.

    Using 0dp (MATCH_CONSTRAINT) as its height is the key. Please read more about it here.

    ... The first two works in a similar fashion as other layouts. The last one will resize the widget in such a way as matching the constraints that are set. If margins are set, they will be taken in account in the computation.

    
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbar"
        app:navGraph="@navigation/nav_graph" />