Skip to content
Advertisement

Hide TextView when CollapsingToolbarLayout is Collapsed, and show it when expanded

I am working on an app using a CollapsingToolbarLayout, with an ImageView inside it. I wanted to add a gradient on top of it to look nicer and to be able to read the CollapsingToolBar title better, so I made a little hack and added a Relative Layout with a textview inside it, then I added a background to that same TextView (which is the gradient I was talking about). The problem with this is that when the ToolBar is collapsed, the gradient still shows over it and I dont want it to happen, how can I make it invisible when the ToolBar is collapsed?

Design:

JavaScript

Images of the design:

ToolBar Expanded

ToolBar Collapsed

Advertisement

Answer

Add an OnOffsetChangedListener to the AppBar and listen to the changes when it’s collapsed or expanded and hide/show your TextView based on that.

Add an ID to the appbar

JavaScript

Then, access the AppBar and add an OnOffsetChangedListener as

JavaScript

You can choose to play with the alpha property only instead of visibility, that’s upto you.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement