Skip to content

Instantly share code, notes, and snippets.

View ValCanBuild's full-sized avatar

Valentin Hinov ValCanBuild

View GitHub Profile
class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
@NestedScrollType
private var lastStartedType: Int = 0
private var offsetAnimator: ValueAnimator? = null
var isSnappingEnabled = false
// rest of the code
@ValCanBuild
ValCanBuild / AppBarLayoutPart.java
Created November 23, 2018 13:11
AppBarLayout onStopNestedScroll method
@Override
public void onStopNestedScroll(
CoordinatorLayout coordinatorLayout, T abl, View target, int type) {
// onStartNestedScroll for a fling will happen before onStopNestedScroll for the scroll. This
// isn't necessarily guaranteed yet, but it should be in the future. We use this to our
// advantage to check if a fling (ViewCompat.TYPE_NON_TOUCH) will start after the touch scroll
// (ViewCompat.TYPE_TOUCH) ends
if (lastStartedType == ViewCompat.TYPE_TOUCH || type == ViewCompat.TYPE_NON_TOUCH) {
// If we haven't been flung, or a fling is ending
snapToChildIfNeeded(coordinatorLayout, abl);
class BottomNavigationFABBehavior(context: Context?, attrs: AttributeSet?) : CoordinatorLayout.Behavior<View>(context, attrs) {
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
return dependency is Snackbar.SnackbarLayout
}
override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
child.translationY = 0f
}
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="@drawable/ic_add_black_24dp"
app:fabSize="normal"
app:layout_anchor="@id/navigation"
app:layout_anchorGravity="top|end"
app:useCompatPadding="true"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="@drawable/ic_add_black_24dp"
app:fabSize="normal"
app:layout_anchor="@id/navigation"
app:layout_anchorGravity="top|end"
app:layout_behavior="com.mypackage.BottomNavigationFABBehavior"
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
@ValCanBuild
ValCanBuild / BottomNavigationBehavior.kt
Last active December 18, 2023 00:04
Full code of a BottomNavigationBehavior which hides itself on scroll and handles Snackbars, FAB and snapping. https://medium.com/@ValCanBuild/scroll-your-bottom-navigation-view-away-with-10-lines-of-code-346f1ed40e9e
/**
MIT License
Copyright (c) 2018 Valentin Hinov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
// Rest of the code is the same
override fun layoutDependsOn(parent: CoordinatorLayout?, child: V, dependency: View?): Boolean {
if (dependency is Snackbar.SnackbarLayout) {
updateSnackbar(child, dependency)
}
return super.layoutDependsOn(parent, child, dependency)
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_behavior="com.mypackage.BottomNavigationBehavior"
app:menu="@menu/my_menu"/>
@ValCanBuild
ValCanBuild / BottomNavigationBehavior.kt
Created March 14, 2018 05:07
BottomNavigationView Behavior - no snackbar support
class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
override fun onStartNestedScroll(
coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int
): Boolean {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL
}
override fun onNestedPreScroll(