Navigation Rail with Material Design 3


The Navigation rail acts as a side navigation component or sidebar and can hold from three, up to seven destination views. Each view consists of an icon and a respective label.
Navigation rails provide access to primary destinations in apps when using tablet and desktop screens.
This component works best on larger screens and can be swapped with a navigation bar on smaller screens.
It makes easy for users to explore and switch between top-level views in a single tap.
It should be placed at the side edge of large screen devices such as tablets, when an application has three to seven top-level destinations.
Menu
The bar contents can be populated by specifying a menu resource file.
You can add menu by using app:menu attribute.
Each menu item title, icon and enabled state will be used for displaying navigation rail bar items.
Menu items can also be used for programmatically selecting which destination is currently active.
Header
A header view can be added with the app:headerLayout attribute.
Color
DynamicColors.applyToActivityIfAvailable(this)
Steps
navigation_rail.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".NavigationRail">
<com.google.android.material.navigationrail.NavigationRailView
android:id="@+id/navigationRail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clipToPadding="false"
app:headerLayout="@layout/navigation_rail_fab"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
style="@style/Widget.Material3.NavigationRailView.ActiveIndicator"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/navigation_rail_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Header
layout/navigation_rail_fab.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.floatingactionbutton.FloatingActionButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_rail_fab"
android:clipToPadding="false"
app:fabSize="auto"
android:contentDescription="@string/todo"
android:backgroundTint="@color/material_dynamic_primary90"
app:srcCompat="@drawable/add_24"/>
Menu
.png)
navigation_rail_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/files"
android:enabled="true"
android:icon="@drawable/folder_open_24"
android:title="files" />
<item
android:id="@+id/images"
android:enabled="true"
android:icon="@drawable/image_24"
android:title="images" />
<item
android:id="@+id/music"
android:enabled="true"
android:icon="@drawable/music_note_24"
android:title="music" />
<item
android:id="@+id/videos"
android:enabled="true"
android:icon="@drawable/movie_24"
android:title="videos" />
</menu>
NavigationRail.kt
NavigationRail.kt
package com.example.androidappcodingblog
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.color.DynamicColors
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.navigationrail.NavigationRailView
class NavigationRail : AppCompatActivity() {
@SuppressLint("MissingInflatedId", "CutPasteId")
override fun onCreate(savedInstanceState: Bundle?) {
//for dynamic colors for this activity only
DynamicColors.applyToActivityIfAvailable(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation_rail)
val nav = findViewById<FloatingActionButton>(R.id.nav_rail_fab)
nav.setOnClickListener {
Toast.makeText(this, "FAB Clicked!", Toast.LENGTH_SHORT).show()
}
val navrail=findViewById<NavigationRailView>(R.id.navigationRail)
navrail.setOnClickListener{ menuItem ->
when (menuItem.id) {
R.id.files -> {
Toast.makeText(this, "Files", Toast.LENGTH_SHORT).show()
}
R.id.images -> {
Toast.makeText(this, "Images", Toast.LENGTH_SHORT).show()
}
R.id.music -> {
Toast.makeText(this, "Music", Toast.LENGTH_SHORT).show()
}
R.id.videos -> {
Toast.makeText(this, "Videos", Toast.LENGTH_SHORT).show()
}
}
}
}
}
OUTPUT


If you enjoyed reading , Show some Love by
clapping 👏 on this post!
0 Comments