Skip to content

Instantly share code, notes, and snippets.

@babedev
Created January 30, 2018 08:37
Show Gist options
  • Save babedev/ba73db1be6f7662168e759f2c70655d3 to your computer and use it in GitHub Desktop.
Save babedev/ba73db1be6f7662168e759f2c70655d3 to your computer and use it in GitHub Desktop.
Disable shift mode for BottomNavigationView
@SuppressLint("RestrictedApi")
fun BottomNavigationView.disableShiftMode() {
val menuView = this.getChildAt(0) as BottomNavigationMenuView
try {
val shiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
shiftingMode.isAccessible = true
shiftingMode.setBoolean(menuView, false)
shiftingMode.isAccessible = false
for (i in 0 until menuView.childCount) {
val item = menuView.getChildAt(i) as BottomNavigationItemView
item.setShiftingMode(false)
item.setChecked(item.itemData.isChecked)
}
} catch (e: NoSuchFieldException) {
e.printStackTrace()
}
}
@LDSOFTLLE
Copy link

object BottomNavigationViewHelper {

@SuppressLint("RestrictedApi")
fun disableShiftMode(view: BottomNavigationView) {
    val menuView = view.getChildAt(0) as BottomNavigationMenuView
    for (i in 0 until menuView.childCount) {
        val item = menuView.getChildAt(i) as BottomNavigationItemView
        item.setShifting(false)
        item.setChecked(item.itemData.isChecked)
    }
}

}

@LDSOFTLLE
Copy link

From API28 this works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment