Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic ConstraintLayout (Toolbar, fragment container, and bottom nav)

I'm adapting to Kotlin and ConstraintLayouts for Android. I'm trying to have a simple layout that has a toolbar and a bottom navigation view.

Here is my current xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="@dimen/toolbar_title_text_size"
            android:textStyle="bold" />

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu_main" />

</android.support.constraint.ConstraintLayout>

Unfortunately, I'm misunderstanding something and cannot seem to get the fragment container to fill the space between the toolbar and the bottom navigation.

Can someone steer me in the correct direction?

like image 381
isuPatches Avatar asked Dec 10 '22 08:12

isuPatches


1 Answers

Height in your fragment container needs to be 0dp, which is the equivalent of "MATCH_CONSTRAINT".

android:layout_height="0dp"

You can look it up here ConstraintLayout.

It should be

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>
like image 94
Mateusz Avatar answered Apr 19 '23 04:04

Mateusz