2

I have a main Activity, which includes a TabLayout inside a CollapsingToolbar. One of my Tabs is a Fragment which contains a FAB in the bottom right hand corner. The issue is that when I go to this Tab, the Collapsing Toolbar is fully open and the FAB is hidden. I can only see it when I Collapse the ToolBar. Is there a way to fix this?

I know that I can place the FAB within my CollapsingToolBar layout, which is in my MainActivity, but I want to/need to keep it in the Fragment Layout instead.

Here is my MainActivity Layout:

<android.support.design.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="wrap_content">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:elevation="24dp"
        android:layout_height="match_parent"
        app:titleEnabled="false"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header_image"
            android:alpha="0.5"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_gravity="top"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            style="@style/AppTabLayout"
           />


    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>



<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:fillViewport="true"
    android:background="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.markfeldman.tasktrack.activities.MainActivity">
</android.support.v4.view.ViewPager>


</android.support.design.widget.CoordinatorLayout>

Here is my Fragment Layout:

<FrameLayout
android:id="@+id/tasks_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.markfeldman.tasktrack.fragments.Tasks">


 <android.support.design.widget.FloatingActionButton
 android:id="@+id/fab"
 app:fabSize="normal"
 android:elevation="6dp"
 app:backgroundTint="@color/colorPrimary"
 android:src="@drawable/ic_add_black_24dp"
 android:layout_gravity="end|bottom"
 app:pressedTranslationZ="12dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="16dp" />

</FrameLayout>

This is what it looks like when I first navigate to my tab and the ToolBar is NOT collapsed:

enter image description here

And Here I pull the ToolBar Up:

enter image description here

I can add marginBottom padding to have it be visible at first but then it looks odd when I Collapse the Toolbar. Any suggestions?

Mark F
  • 1,523
  • 3
  • 23
  • 41

1 Answers1

0

Move FAB to activity layout:

<android.support.design.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="wrap_content">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:elevation="24dp"
    android:layout_height="match_parent"
    app:titleEnabled="false"
    android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/header_image"
        android:alpha="0.5"
        android:scaleType="centerCrop"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_gravity="top"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        style="@style/AppTabLayout"/>

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:fillViewport="true"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.markfeldman.tasktrack.activities.MainActivity">
    </android.support.v4.view.ViewPager>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:fabSize="normal"
        android:elevation="6dp"
        app:backgroundTint="@color/colorPrimary"
        android:src="@drawable/ic_add_black_24dp"
        app:pressedTranslationZ="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        <!--NEXT 2 LINES SHOULD HELP-->
        app:layout_anchor="@id/content"
        app:layout_anchorGravity="bottom|end"/>

</android.support.design.widget.CoordinatorLayout>
Jurij Pitulja
  • 5,546
  • 4
  • 19
  • 25