Search code examples
androidmaterial-designandroid-toolbarandroid-themeandroid-collapsingtoolbarlayout

Change Collapsing toolbar menu icons color on scroll


How to create Toolbar like Google trips application Trip Screen [Please check the image in the below link]. https://i.sstatic.net/kZVnC.jpg ,

Before scroll toolbar menu icons will be in white colour. After scroll toolbar is collapsed menu icons will be changed to grey colour.


Solution

  • To change toolbar icons color on scroll, I used ToolbarColorizeHelper .[https://gist.github.com/chomi3/7e088760ef7bca10430e][1]

    i added the addOnOffsetChangedListener to collapsing toolbar to observe the toolbar scroll offset. Below is the code I used to achieve the expected UI attached in the above question.

    addOnOffsetChangedListener((appBarLayout, verticalOffset) -> { //Check if the view is collapsed

           if (scrollOffset <= toolbar.getHeight()) {
                    ToolBarColourizer.colorizeToolbar(toolbar, Color.BLACK);             
            } else {
                    ToolBarColourizer.colorizeToolbar(toolbar, Color.WHITE);
                }
            }
        });