zoo
zoo

Reputation: 21

How to fix FAB in Fragment in common Coordinator Layout

How to fix FAB in Fragment in common CoordinatorLayout

I want to know the best or better practice about handling view (ex.Floating Action Button)in this situation.

Problem

At present, I implement a common Toolbar that moves like hiding or appearing in Activity for displaying in some Fragments. Then, I want to implement a FAB at a particular Fragment. But when I implemented a FAB in the Fragment, it moved the amount of height of ToolBar.

Fail Image

I want to...

I want it to fix at the bottom and right on the screen.

How can I solve this problem? If you don't mind, please tell me about this.

I want to...

My Thought

I think that FAB should be put in Activity. If things go well, I want to change visibility and different roles depending on the Fragment. and is it possible...?(as Twitter)

My English is poor, but I appreciate your kindness.

Code(excerpt)

in Activity

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

        <androidx.appcompat.widget.Toolbar         
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"           
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

in Fragment


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            -- some constraints--
            android:layout_margin="16dp"/>
</<androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 2

Views: 684

Answers (1)

lincollincol
lincollincol

Reputation: 862

Use android:layout_gravity="bottom|start" instead of layout_constraintBottom_toBottomOf to fix your FAB at the bottom|start of your layout. You can use constraints only in ConstraintLayout.

If you want to use CoordinatorLayout, you should use android:layout_gravity.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:layout_gravity="bottom|start"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

And make sure that you are using androidx in your project. If not, change

androidx.coordinatorlayout.widget.CoordinatorLayout

to

android.support.design.widget.CoordinatorLayout

In your question, you are using ConstraintLayout in fragment xml. Change it to CoordinatorLayout

Update

Use ConstraintLayout in your activity XML. Change your XML to this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:fitsSystemWindows="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

    <fragment
        android:id="@+id/fragmentContainer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/appBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout="@layout/your_fragment_layout" />

</androidx.constraintlayout.widget.ConstraintLayout>

And the fragment does not need to be changed.

Upvotes: 1

Related Questions