Skip to content Skip to sidebar Skip to footer

How Do You Change The Color Of Collapsing Toolbar When It's Collapsed?

I have a collapsing toolbar, but I cannot for the love of god figure out why its not changing to the color i want it to as it collapses...Here is my xml code the collapsing toolbar

Solution 1:

add a addOnOffsetChangedListener listener in the AppBarLayout to know if it's collapse or not, and change the color using setBackgroundColor. Like this:

//Set a listener to know the current visible state of CollapseLayout
appBarLayout.addOnOffsetChangedListener(newAppBarLayout.OnOffsetChangedListener() {
    intscrollRange= -1;

    @OverridepublicvoidonOffsetChanged(final AppBarLayout appBarLayout, int verticalOffset) {
        //Initialize the size of the scrollif (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        //Check if the view is collapsedif (scrollRange + verticalOffset == 0) {
            toolbar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.YOUR_COLLAPSED_COLOR));
        }else{
            toolbar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.OTHER_COLOR));
        }
    }
});

Solution 2:

The selected answer doesn´t work for me, looks terrible in my case when the color changes when verticalOffset == 0, so I tried this:

collapsingToolbarLayout.setContentScrimColor(Color.parseColor("#000000")); //#000000 is black color hex (for example)

And now changing color when collapsed looks smooth and clean,

Hope it helps somebody,

Post a Comment for "How Do You Change The Color Of Collapsing Toolbar When It's Collapsed?"