How to implement fragment navigation using navigation drawer in Kotlin Android
How to implement fragment navigation using navigation drawer in Kotlin Android.
Here's a step-by-step tutorial on how to implement fragment navigation using a navigation drawer in Kotlin for Android:
Step 1: Create a new Android project
First, create a new Android project in Android Studio. Choose an appropriate project name and package name.
Step 2: Add the necessary dependencies
In your app-level build.gradle file, add the following dependencies:
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.0'
Step 3: Create the navigation graph
Create a new XML file called nav_graph.xml
in the res/navigation
directory. This file will define the navigation graph for your fragments.
Define the start destination and add fragments as destinations using the <fragment>
tag. For example:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.app.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/settingsFragment"
android:name="com.example.app.SettingsFragment"
android:label="Settings"
tools:layout="@layout/fragment_settings" />
</navigation>
Step 4: Create the navigation drawer layout
In your main activity layout file, add a DrawerLayout
as the root element. Inside the DrawerLayout
, add a NavigationView
for the navigation drawer content and a FrameLayout
for the fragment container.
For example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_navigation" />
</androidx.drawerlayout.widget.DrawerLayout>
Step 5: Create the navigation drawer menu
Create a new XML file called menu_navigation.xml
in the res/menu
directory. This file will define the items for the navigation drawer menu.
For example:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="@+id/menu_home"
android:title="Home" />
<item
android:id="@+id/menu_settings"
android:title="Settings" />
</group>
</menu>
Step 6: Set up the navigation drawer in the main activity
In your main activity class, initialize the navigation drawer by finding the DrawerLayout
and NavigationView
views. Set up the navigation drawer by creating a ActionBarDrawerToggle
and syncing it with the DrawerLayout
.
Set the navigation drawer item selected listener to handle item clicks and navigate to the corresponding fragment.
For example:
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var navigationView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
drawerLayout = findViewById(R.id.drawerLayout)
navigationView = findViewById(R.id.navigationView)
val toggle = ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.menu_home -> {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, HomeFragment())
.commit()
drawerLayout.closeDrawer(GravityCompat.START)
true
}
R.id.menu_settings -> {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, SettingsFragment())
.commit()
drawerLayout.closeDrawer(GravityCompat.START)
true
}
else -> false
}
}
}
}
Step 7: Handle back button press
To handle the back button press correctly, override the onBackPressed
method in your main activity and close the navigation drawer if it is open.
For example:
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
That's it! You have successfully implemented fragment navigation using a navigation drawer in Kotlin for Android. You can now add more fragments to the navigation graph and handle their navigation in the main activity.