Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Programmatically Collapse and expand the CollapsingToolbarLayout

Tags:

android

I have ImageView and TabLayout(4 Tabs) inside CollapsingToolbarLayout, Now i want to collapse Appbar when clicking on Tabs(2,3,4) and for first tab it should work normally(as per scrolling). Is there a way to expand and collapse Appbar programmatically?

however i have seen solution, appBarLayout.setExpanded(false) collapses Appbar but again it is able to drag down. i want to prevent AppBar Expansion until Tab 1 is clicked?

like image 426
user3912899 Avatar asked Apr 26 '16 06:04

user3912899


People also ask

What is collapsingtoolbarlayout in Android?

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout.

How do I expand and collapse the toolbar in androidx?

Since the v23 of Support (proceeding to AndroidX), there is setExpanded method in the AppBarLayout. Use mAppBarLayout.setExpanded (true) to expand Toolbar and use mAppBarLayout.setExpanded (false) to collapse Toolbar.

Is it possible to expand/collapse the collapsibletoolbarlayout without animation?

I've written a small extension to AppBarLayout. It allows for expanding and collapsing of the CollapsibleToolbarLayout both with and without animation. It seems to be doing it quite right. Feel free to try it out.

How to change the height of the appbarlayout in androidx?

Since the v23 of Support (proceeding to AndroidX), there is setExpanded method in the AppBarLayout. Use mAppBarLayout.setExpanded (true) to expand Toolbar and use mAppBarLayout.setExpanded (false) to collapse Toolbar. If you want to change CollapsingToolbarLayout height programmatically then just use mAppBarLayout.setLayoutParams (params);


1 Answers

Use mAppBarLayout.setExpanded(true) to expand Toolbar and use mAppBarLayout.setExpanded(false) to collapse Toolbar.

If you want to prevent CollapsingToolbarLayout expansion until Tab 1 is clicked then you should use mAppBarLayout.setLayoutParams(params) programmatically to change CollapsingToolbarLayout height.

Collapse: Use when Tabs(2,3,4) clicked

CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // COLLAPSED_HEIGHT

mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);

Expand: Use when Tab 1 clicked

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // EXPANDED_HEIGHT

mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);

Hope this will help you~

like image 51
Ferdous Ahamed Avatar answered Sep 27 '22 18:09

Ferdous Ahamed