Creating a bottom navigation bar in Android using Kotlin is an essential part of designing a user-friendly mobile application. This component helps users navigate easily between key sections of the app like Home, Search, and Profile. Android’s BottomNavigationView
, a part of the Material Design Components, provides a seamless solution for implementing this navigation pattern.
To get started, ensure you’ve added the Material Components dependency in your build.gradle
file:
implementation 'com.google.android.material:material:1.11.0'
implementation 'com.google.android.material:material:1.11.0'
implementation 'com.google.android.material:material:1.11.0'
Here’s a complete Kotlin code example using BottomNavigationView
:
1. XML Layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled" />
</RelativeLayout>
2. Menu Resource (res/menu/bottom_nav_menu.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
<item android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="Search" />
<item android:id="@+id/nav_profile"
android:icon="@drawable/ic_profile"
android:title="Profile" />
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="Search" />
<item android:id="@+id/nav_profile"
android:icon="@drawable/ic_profile"
android:title="Profile" />
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="Search" />
<item android:id="@+id/nav_profile"
android:icon="@drawable/ic_profile"
android:title="Profile" />
</menu>
3. MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener { item ->
loadFragment(HomeFragment())
loadFragment(SearchFragment())
loadFragment(ProfileFragment())
loadFragment(HomeFragment())
private fun loadFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_home -> {
loadFragment(HomeFragment())
true
}
R.id.nav_search -> {
loadFragment(SearchFragment())
true
}
R.id.nav_profile -> {
loadFragment(ProfileFragment())
true
}
else -> false
}
}
// Load default fragment
loadFragment(HomeFragment())
}
private fun loadFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit()
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_home -> {
loadFragment(HomeFragment())
true
}
R.id.nav_search -> {
loadFragment(SearchFragment())
true
}
R.id.nav_profile -> {
loadFragment(ProfileFragment())
true
}
else -> false
}
}
// Load default fragment
loadFragment(HomeFragment())
}
private fun loadFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.commit()
}
}
With just these steps, you’ve successfully added a bottom navigation bar using Kotlin in Android Studio. It improves the UX significantly, especially for apps with multiple main features.
For further customization and design tips, refer to the official Android Material Design guide.
Post Views: 70