Reputation: 2862
I have a collapsing toolbar layout. I have shown graph on it. So if there are no items to show in the graph I want to show the collapsing toolbar layout as collapsed or hidden or closed. Only the toolbar and title should be displayed and not the graph.
Here is the view :
I want to display only the toolbar where I have a menu icon on left side.
Layout code :
<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"
android:layout_gravity="center"
tools:context="com.example.siddhi.googletaskmanager"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="@+id/parentPanel"
android:background="@color/background">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="@+id/appbar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:expandedTitleTextAppearance="@style/expandedappbar"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/toolbar"
app:layout_collapseMode="parallax"
android:layout_gravity="bottom"
android:layout_marginBottom="40dp"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:paddingTop="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/menu1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
app:menu_colorNormal="@color/colorAccent"
app:menu_colorPressed="@color/colorAccent"
fab:menu_icon="@drawable/ic_more_horiz_white_24dp"
fab:menu_labels_ellipsize="end"
fab:menu_labels_singleLine="true"
fab:menu_backgroundColor="@color/colorPrimary"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="List 1"
app:fab_colorPressed="@color/colorAccent"
app:fab_colorNormal="@color/colorAccent"/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="List 2"
app:fab_colorPressed="@color/colorAccent"
app:fab_colorNormal="@color/colorAccent"/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="List 3"
app:fab_colorPressed="@color/colorAccent"
app:fab_colorNormal="@color/colorAccent"/>
</com.github.clans.fab.FloatingActionMenu>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_24dp"
fab:fab_size="normal"
app:fab_colorPressed="@color/colorAccent"
app:fab_colorNormal="@color/colorAccent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="80dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="10dp" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
EDIT:
My current code is this :
if(mItems.size() == 0) {
mBarChart.setVisibility(View.INVISIBLE);
appBarLayout.setExpanded(false,false);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
// lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);
lp.height = (int) (android.R.attr.actionBarSize);
}
else
{
appBarLayout.setExpanded(true);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
lp.height = (int) getResources().getDimension(R.dimen.appbar_height);
mBarChart.setVisibility(View.VISIBLE);
}
dimension:
<dimen name="toolbar_height">128dp</dimen>
<dimen name="appbar_height">200dp</dimen>
How can I achieve this? Thank you..
Upvotes: 3
Views: 8091
Reputation: 1710
You can do this programatically using appBarLayout.setExpanded(true/false)
.
if(yourGraphIsEmpty){
appBarLayout.setExpanded(false);
}else{
appBarLayout.setExpanded(true);
}
The core problem is that there is no CollapsingToolbarLayout.lock(); method up until now (v23.2.1 of support design)
This post is a complete solution that really works well
Upvotes: 5