user3912899
user3912899

Reputation: 65

Android: Programmatically Collapse and expand the CollapsingToolbarLayout

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?

Upvotes: 5

Views: 7043

Answers (1)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

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~

Upvotes: 9

Related Questions