Coordinator Layout with Toolbar in Fragments or Activity

Coordinator Layout with Toolbar in Fragments or Activity

As for me it sounds too weird to have appbar and toolbar in each fragment. So I've chosen to have single appbar with toolbar in activity.

To solve that issue with CoordinatorLayout you will have to set different behaviour of your FrameLayout (or any other Layout) that supposed to hold fragments from each fragment that you want to override default behaviour.

Lets assume, that your default behaviour is app:layout_behavior="@string/appbar_scrolling_view_behavior"

Then in your fragment_activity_layout.xml you may have something like that:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/dashboard_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

<FrameLayout
android:id="@+id/dashboard_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

And in each fragment you wish not to implement app:layout_behavior="@string/appbar_scrolling_view_behavior" you will have to override onAttach and onDetach methods that will change behaviour of your FrameLayout:

CoordinatorLayout.Behavior behavior;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

if(behavior != null)
return;

FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

behavior = params.getBehavior();
params.setBehavior(null);

}

@Override
public void onDetach() {
super.onDetach();
if(behavior == null)
return;

FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();

params.setBehavior(behavior);

layout.setLayoutParams(params);

behavior = null;
}

After that CoordinatorLayout won't collapse appbar, etc. and will allow fragment layouts to be full-height.

Android - Toolbar under status bar in CoordinatorLayout in Fragment

I had the same issue.
Put:

android:fitsSystemWindows="true"

in the root of your Activity's layout(the Activity that contains the fragment), not in the root of the fragment's layout(in your case the CoordinatorLayout).

Using CoordinatorLayout in fragments with ActionBar in Activity

I have Activity with DrawerLayout and Fragments with CoordinatorLayout working just fine (pay attention where fitsSystemWindows attributes are set)

here is activity layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/color_primary" />

<android.support.design.widget.NavigationView
android:id="@+id/menu_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bm_white"
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
android:dividerHeight="0dp"
android:divider="@null" />
<!-- Картинки - fb, vk, twitter -->

</LinearLayout>

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

</android.support.v4.widget.DrawerLayout>

and fragment's xml with coordinator, action button etc:

<?xml version="1.0" encoding="utf-8"?>
<!--NOT SET HERE: android:fitsSystemWindows="true"-->
<android.support.design.widget.CoordinatorLayout
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">

<!-- NOT SET HERE: android:fitsSystemWindows="true" -->
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">

<ImageView
android:id="@+id/backdrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/pic_top_main" />

</RelativeLayout>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
app:behavior_overlapTop="32dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:id="@+id/ll_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- In code now… -->
<!--<include layout="@layout/fragment_start_nonfc"/>-->
<!--<include layout="@layout/fragment_start"/>-->

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:onClick="onClickPurchase_Remote"
android:src="@drawable/btn_purchase"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:pressedTranslationZ="12dp" />

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

CoordinatorLayout collapse Toolbar in Activity from Fragment

  1. Use NestedScrollView instead of ScrollView in the Fragment xml.
  2. Set the attributte
    app:layout_behavior="@string/appbar_scrolling_view_behavior" to the
    NestedScrollView instead of adding it to the fragment container.

A full working example:

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">

<fragment
android:id="@+id/fragment"
android:name="io.victoralbertos.stackoverflow.ChildFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/child_fragment"/>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>

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

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

child_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:textSize="150sp"
android:text="Scroll me as much as you need"
android:layout_height="match_parent"/>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

CoordinatorLayout hide toolbar when scrolling only in specific fragment

@Yonatan's answer in my case was not enough, when navigating between fragments some views were not visible in the bottom, probably some height misconfiguration.

Enabling and disabling scroll flags for Toolbar works just fine.

public void disableToolbarScrollBehavior() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);
}

public void enableToolbarScrollBehavior() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
}

How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

I would assume that the xml you posted is your layout for your MainActivity(), if so create a reference to your Toolbar in your MainActivty and create a function in your MainActivty() to access/change your Toolbar collapse states.

In your Fragments, create a reference to your MainActivity to then access the function to change the state of your Toolbar.

I cannot test as from work, but I have used same code flow to do the same stuff when using only MainActivity with Inner Fragments as the navigation flow of the app.

Below not exact working or tested code, but same idea/flow

//my fragment function to access MainActivity().changeToolbarState(boolean)
changeToolbar(state: boolean) {
if(state){
//true
//show toolbar
MainActivty().changeToolbarState(true)

}else{
//false
//collapse/hidetoolbar
MainActivty().changeToolbarState(false)
}
}


Related Topics



Leave a reply



Submit