Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
set this on your scrolling view for which you wish to have the behavior applied:
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
* Extension of standard [AppBarLayout.ScrollingViewBehavior] which
* ensures the scrollview bottom aligns with the top of the view below it,
* such as a BottomNavigationView
class ScrollingViewAboveBottomNavigationBehavior @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
AppBarLayout.ScrollingViewBehavior(context, attrs) {
private var marginBottom = 0
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean =
super.layoutDependsOn(parent, child, dependency) || dependency is BottomNavigationView
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
return if (dependency is BottomNavigationView && dependency.getMeasuredHeight() != marginBottom) {
marginBottom = dependency.getMeasuredHeight()
child.layoutParams = (child.layoutParams as CoordinatorLayout.LayoutParams).apply {
bottomMargin = marginBottom
} else super.onDependentViewChanged(parent, child, dependency)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.