Skip to content

Instantly share code, notes, and snippets.

@Kaspic
Last active March 4, 2021 20:35
Show Gist options
  • Save Kaspic/b6b6f725abb498ac75dd8b2aa3bd300f to your computer and use it in GitHub Desktop.
Save Kaspic/b6b6f725abb498ac75dd8b2aa3bd300f to your computer and use it in GitHub Desktop.
// If you don't add a number, only a badge will be displayed at the respective menu_item
var badge = findViewById<NavigationRailView>(R.id.navigation_rail).getOrCreateBadge(R.id.menu_item_account)
badge.isVisible = true
// In this case, the number 99 will be displayed in the badge
badge = findViewById<NavigationRailView>(R.id.navigation_rail).getOrCreateBadge(R.id.menu_item_moon)
badge.isVisible = true
badge.number = 99
dependencies {
// ...
implementation 'com.google.android.material:material:1.4.0-alpha01'
// ...
}
NavigationRailView.OnNavigationItemSelectedListener { item ->
when(item.itemId) {
R.id.menu_item_account -> {
// Account item in NavigationRail clicked
true
}
R.id.menu_item_moon -> {
// Moon item in NavigationRail clicked
true
}
R.id.menu_item_time -> {
// Time item in NavigationRail clicked
true
}
else -> false
}
}
<com.google.android.material.navigationrail.NavigationRailView
android:id="@+id/navigation_rail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:headerLayout="@layout/navigation_rail_header"
app:menu="@menu/navigation_rail_menu" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_account"
android:enabled="true"
android:icon="@drawable/ic_account"
android:title="@string/menu_item_account_label"/>
<item
android:id="@+id/menu_item_moon"
android:enabled="true"
android:icon="@drawable/ic_moon"
android:title="@string/menu_item_moon_label"/>
<item
android:id="@+id/menu_item_time"
android:enabled="true"
android:icon="@drawable/ic_time"
android:title="@string/menu_item_time_label"/>
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment