Skip to content

Instantly share code, notes, and snippets.

@samuel22gj
Last active August 7, 2020 08:29
Show Gist options
  • Save samuel22gj/de69d28db0240daea8ac2548f239207f to your computer and use it in GitHub Desktop.
Save samuel22gj/de69d28db0240daea8ac2548f239207f to your computer and use it in GitHub Desktop.
/**
* A customized [BottomNavigationView] with some convenience functions.
*/
class BottomNavigationViewPlus @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = com.google.android.material.R.attr.bottomNavigationStyle
) : BottomNavigationView(
context, attrs, defStyleAttr
) {
fun setBadgeVisible(@IdRes menuItemId: Int, visible: Boolean) {
getOrCreateBadge(menuItemId).isVisible = visible
}
fun showBadgeWithoutNumber(@IdRes menuItemId: Int) {
getOrCreateBadge(menuItemId).let { badge ->
badge.isVisible = true
badge.clearNumber()
}
}
fun showBadgeWithNumber(@IdRes menuItemId: Int, number: Int) {
getOrCreateBadge(menuItemId).let { badge ->
badge.isVisible = true
badge.number = number
}
}
fun getItemView(@IdRes menuItemId: Int): View? = children
.filterIsInstance(BottomNavigationMenuView::class.java)
.flatMap { it.children }
.firstOrNull { it.id == menuItemId }
fun disableTooltip() = children
.filterIsInstance(BottomNavigationMenuView::class.java)
.flatMap { it.children }
.forEach { TooltipCompat.setTooltipText(it, null) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment