Search code examples
android-layoutmaterial-designandroid-toolbarandroid-collapsingtoolbarlayoutandroid-appbarlayout

What's the use of CollapsingToolbarLayout's toolbarId attribute?


What is CollapsingToolbarLayout's toolbarId for? I can't find any description in Google and my AppBarLayout/CollapsingToolbarLayout works properly without it.


Solution

  • I figured it out myself by looking into the source code of CollapsingToolbarLayout (CTBL). CTBL in fact doesn't need a toolbarId set explicitly because it searches for a toolbar in its child views if no id is set. However, by setting a toolbarId we can optimize our code because then the CTBL doesn't have to search itself (which happens repeatedly).

    enter image description here https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java