A bottom navigation bar helps users switch between main sections of an app quickly. It stays at the bottom of the screen for easy access.
Bottom navigation bar in Android Kotlin
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.home -> {
// Handle home click
true
}
R.id.search -> {
// Handle search click
true
}
else -> false
}
}Use BottomNavigationView from the Material Components library.
Set an OnItemSelectedListener to respond when users tap items.
bottomNavigationView.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.home -> {
// Show home screen
true
}
R.id.profile -> {
// Show profile screen
true
}
else -> false
}
}<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" />This app shows a bottom navigation bar with three items. When you tap an item, a small message appears at the bottom.
package com.example.bottomnav import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.google.android.material.bottomnavigation.BottomNavigationView import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation) bottomNavigationView.setOnItemSelectedListener { item -> when (item.itemId) { R.id.home -> { Toast.makeText(this, "Home selected", Toast.LENGTH_SHORT).show() true } R.id.dashboard -> { Toast.makeText(this, "Dashboard selected", Toast.LENGTH_SHORT).show() true } R.id.notifications -> { Toast.makeText(this, "Notifications selected", Toast.LENGTH_SHORT).show() true } else -> false } } } }
Make sure to add the Material Components library in your build.gradle file.
Define your menu items in a separate XML file under res/menu for easy management.
Use icons and labels that clearly describe each section for better user experience.
Bottom navigation bars help users switch between main app sections easily.
Use BottomNavigationView with a menu resource and set a listener for item taps.
Keep the number of items between 3 and 5 for best usability.
Practice
BottomNavigationView in an Android app?Solution
Step 1: Understand BottomNavigationView role
The BottomNavigationView is designed to provide quick navigation between top-level views in an app.Step 2: Compare options with its purpose
Options B, C, and D describe unrelated features like notifications, image galleries, or background tasks.Final Answer:
To let users switch between main sections of the app easily -> Option AQuick Check:
BottomNavigationView = Easy app section switching [OK]
- Confusing BottomNavigationView with notification or image display
- Thinking it handles background processes
- Assuming it is a full-screen component
BottomNavigationView in Kotlin?Solution
Step 1: Recall BottomNavigationView listener method
The correct method to listen for item selection issetOnItemSelectedListener.Step 2: Check method names in options
Options A, C, and D use incorrect method names not defined for BottomNavigationView.Final Answer:
bottomNav.setOnItemSelectedListener { item -> true } -> Option BQuick Check:
Listener method = setOnItemSelectedListener [OK]
- Using setOnClickListener instead of setOnItemSelectedListener
- Confusing with setOnItemClickListener (not valid here)
- Misspelling the listener method name
bottomNav.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.home -> { showHome(); true }
R.id.search -> { showSearch(); true }
else -> false
}
}
What happens when the user taps the "search" item?Solution
Step 1: Analyze the when block for itemId
When the itemId is R.id.search, the code calls showSearch() and returns true.Step 2: Understand return value meaning
Returning true means the tap event is handled properly, so showSearch() runs.Final Answer:
The showSearch() function is called and the tap is handled -> Option DQuick Check:
ItemId search triggers showSearch() [OK]
- Assuming else case runs for known items
- Thinking returning false still triggers action
- Confusing itemId values
bottomNav.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.home -> showHome()
R.id.profile -> showProfile()
}
}Solution
Step 1: Check lambda return type requirement
The listener expects a Boolean return indicating if the tap was handled.Step 2: Identify missing return statements
The when cases call functions but do not return true or false, causing a type mismatch.Final Answer:
Missing return value for the lambda, causing a type mismatch error -> Option CQuick Check:
Listener lambda must return Boolean [OK]
- Forgetting to return true after handling item
- Using setOnClickListener instead of setOnItemSelectedListener
- Ignoring compiler errors about return type
Solution
Step 1: Recall BottomNavigationView usability guidelines
Best practice is to have between 3 and 5 items with clear icons and labels.Step 2: Evaluate options against guidelines
Add 6 items to show more options at once exceeds recommended items; C removes labels which hurts clarity; D places it incorrectly at top.Final Answer:
Use exactly 4 items and provide clear icons and labels for each -> Option AQuick Check:
3-5 items with icons and labels = best practice [OK]
- Adding too many items causing clutter
- Removing labels making navigation unclear
- Placing BottomNavigationView at top instead of bottom
Ready to prove it?
Pass the test and this pattern moves unknown
