Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coordinator Layout with Toolbar in Fragments or Activity

With the new design library there are several new layouts that change a lot how the toolbar can behave if the developer so wishes. Since different fragments have different behaviors and objectives, for example a gallery fragment with a collapsing toolbar showing an important photo, or a fragment without a scrollview that just doesn't need the appbarlayout for hiding the toolbar, having a single toolbar in the activity can prove difficult.

So with this, should I move the toolbar to each fragment? If so, I have to set the supportActionBar each time I show a fragment and also have a reference of the activity in the fragment which nullifies the independent nature of fragments. If I leave the toolbar in the Activity alone, I have to have multiple layouts defined for each type of behavior in each fragment. What would be the best approach?

like image 392
mthandr Avatar asked Jun 09 '15 18:06

mthandr


People also ask

How do I access the activity toolbar in fragment?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.

What is the coordinator layout?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

Which layout is used in fragment?

A FrameLayout is used because it's going to be the container of the Fragment. In other words, the Fragment will be stored inside of this layout.

How do I change the layout of a fragment?

You cannot replace the fragment's layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like Fragments .


2 Answers

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.

like image 98
Клаус Шварц Avatar answered Oct 13 '22 14:10

Клаус Шварц


Here's my solution

<!-- Put your fragment inside a Framelayout and set the behavior for this FrameLayout --> <FrameLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior">      <!-- Your fragment -->     <include layout="@layout/content_main" />  </FrameLayout>  <android.support.design.widget.AppBarLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:theme="@style/AppTheme.AppBarOverlay">      <android.support.v7.widget.Toolbar         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="?attr/actionBarSize"         android:background="?attr/colorPrimary"         app:layout_scrollFlags="scroll|enterAlways"         app:popupTheme="@style/AppTheme.PopupOverlay" />  </android.support.design.widget.AppBarLayout> 

like image 36
Charles-Eugene Loubao Avatar answered Oct 13 '22 16:10

Charles-Eugene Loubao