Collapsible toolbar programatically in android

--

Sometimes may need the toolbar collapsible in the initial stage or in a certain condition. In that time we implement the functionality programmatically. Here is the following code or function we can call in a particular condition.

fun setCollapsibleToolbar() {
val params = appBar.layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as AppBarLayout.Behavior?
if (behavior != null) {
val valueAnimator: ValueAnimator = ValueAnimator.ofInt()
valueAnimator.interpolator = DecelerateInterpolator()
valueAnimator.addUpdateListener { animation ->
behavior.topAndBottomOffset = (animation.animatedValue as Int)!!
appBar.requestLayout()
}
valueAnimator.setIntValues(0, -900)
valueAnimator.duration = 400
valueAnimator.start()
}
}

--

--

Mehedi Hassan Piash
Mehedi Hassan Piash

Written by Mehedi Hassan Piash

Sr. Software Engineer | Android | iOS | KMP | Jetpack Compose | React-Native.

No responses yet