Search code examples
androidandroid-layoutandroid-coordinatorlayout

Bottom of ConstraintLayout activity not visible


I have layout files:

<?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">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/top_app_bar_activity_pool"
            app:navigationIcon="@drawable/logo_24dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            />

    </com.google.android.material.appbar.AppBarLayout>
    <include layout="@layout/content_activity_inspection_details" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

and @layout/content_activity_inspection_details:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <com.google.android.material.button.MaterialButton
            android:id="@+id/b_navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Navigate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

In activity I have:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_details);
        Toolbar toolbar = findViewById(R.id.topAppBar);
        setSupportActionBar(toolbar);

In Manifest:

<activity
            android:name=".view.activity.InspectionDetailsActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />

Problem: is that tv_name is not visible - it is under toolbar.

I can add app:layout_behavior="@string/appbar_scrolling_view_behavior" which is com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior and that results in that tv_name is visible but the button is not then.

What should I change to have my ConstraintLayout starting below a toolbar and ending at the bottom of a screen?


Solution

  • You need to add exitUntilCollapsed flag to the AppBarLayout flags

    app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"
    

    And add below behavoiur to the layout below the AppBarLayout which is the ConstraintLayout

    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    

    So your layouts will be:

    <?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">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:liftOnScroll="true">
    
            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"
                app:menu="@menu/top_app_bar_activity_pool"
                app:navigationIcon="@drawable/logo_24dp" />
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <include layout="@layout/content_activity_inspection_details" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    And @layout/content_activity_inspection_details:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
        <com.google.android.material.button.MaterialButton
            android:id="@+id/b_navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Navigate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>