Building a Bottom Navigation Bar with Fragments in Android
Bottom navigation gives users a clear way to move between the main areas of an Android app. It works especially well when each destination represents a primary task, such as Home, Search, Saved Items and Settings. By connecting the navigation bar to fragments, you can update the content area without recreating the entire activity.
This pattern suits beginner projects and production applications alike. A local events app for Sydney, a Melbourne public transport guide or a Brisbane café finder can use the same structure while presenting different content. The example below uses Kotlin, XML layouts and AndroidX components.
Plan The Navigation Structure
A bottom navigation bar should contain three to five top-level destinations. Too many items make labels difficult to read and can cause poor touch-target spacing on smaller phones. For an Australian retail app, destinations might be Shop, Orders, Favourites and Account; a travel app could use Explore, Tickets, Alerts and Profile.
Create a fragment class for every destination:
class HomeFragment : Fragment(R.layout.fragment_home)
class SearchFragment : Fragment(R.layout.fragment_search)
class SavedFragment : Fragment(R.layout.fragment_saved)
class SettingsFragment : Fragment(R.layout.fragment_settings)
Each fragment owns its screen layout and related behaviour. Keeping responsibilities separate makes it easier to add a SQLite-backed saved list, a SharedPreferences setting or a RecyclerView later without turning MainActivity into one large class.
Add Material Components
In app/build.gradle, add the AndroidX fragment library and Material Components dependency. Versions change over time, so use the current stable releases available in Android Studio:
implementation("androidx.fragment:fragment-ktx:<version>")
implementation("com.google.android.material:material:<version>")
Use a Material theme in themes.xml, then place a FragmentContainerView above a BottomNavigationView in the activity layout. The container displays the selected fragment, while the navigation component handles item taps.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav_menu" />
For a screen used on Perth buses or Melbourne trams, keep icons recognisable and labels short. Good contrast matters in bright outdoor conditions, while a sufficiently large touch area helps users operate the app while standing or moving.
Define Menu Items And Icons
Create res/menu/bottom_nav_menu.xml. Every item needs a stable ID, a readable title and an icon. Android Studio includes several Material icons, and you can import vector assets through the Resource Manager.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="@string/search" />
<item
android:id="@+id/nav_saved"
android:icon="@drawable/ic_bookmark"
android:title="@string/saved" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />
</menu>
Put titles in strings.xml so they can be translated and tested independently. Avoid relying on colour alone to show selection, since accessibility services and users with colour-vision differences need another visual cue. Content descriptions should describe the destination rather than the icon itself.
Connect The Bar To Fragments
In MainActivity, obtain the navigation view and display the initial fragment. The listener returns true when the selected item is handled. replace() changes the visible screen inside the container.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, HomeFragment())
.commit()
}
findViewById<BottomNavigationView>(R.id.bottom_navigation)
.setOnItemSelectedListener { item ->
val fragment = when (item.itemId) {
R.id.nav_home -> HomeFragment()
R.id.nav_search -> SearchFragment()
R.id.nav_saved -> SavedFragment()
R.id.nav_settings -> SettingsFragment()
else -> return@setOnItemSelectedListener false
}
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
true
}
}
}
| Requirement | Recommended approach | Reason |
|---|---|---|
| Initial destination | Show Home after the first activity creation | Gives users a predictable starting point |
| Screen switching | Use FragmentManager transactions |
Keeps content inside one activity |
| Selection state | Let BottomNavigationView manage checked items |
Provides familiar visual feedback |
| Temporary input | Store values in a ViewModel | Helps preserve data during rotation |
| Deep links | Select the matching menu item | Makes external navigation understandable |
Creating a new fragment on every tap is acceptable for a small tutorial, but a larger app should consider navigation components and shared ViewModels. These tools preserve state more reliably when a user rotates a phone or returns from another activity.
Preserve State During Rotation
Configuration changes can recreate the activity when the device rotates, the font scale changes or the system applies a display adjustment. The savedInstanceState check prevents the initial fragment from being inserted over an already-restored fragment.
A ViewModel is useful for search text, selected filters and loaded records. For durable data, use Room or another repository layer rather than storing database rows directly in the activity. A fragment can observe state with repeatOnLifecycle, allowing the UI to stop collecting data when it is no longer visible.
Bottom navigation is generally intended for top-level destinations, so avoid placing a second bottom bar inside a child fragment. If a destination needs detail pages, use a toolbar back button, a nested navigation graph or a standard fragment transaction.
Improve Accessibility And Visual Feedback
Set meaningful labels, use accessible contrast and test the interface with TalkBack. The selected item should be obvious through the checked state, icon treatment and text styling. Avoid tiny custom icons that become unclear on lower-density screens or when Android magnification is enabled.
Scrollable content inside a destination should not be hidden behind the navigation bar. Apply appropriate bottom padding or window inset handling, especially when edge-to-edge layouts are enabled. A practical reference for adjusting Android scroll indicators is this guide to vertical scrollbar styling, which can help keep a long ListView or RecyclerView readable.
Test common Australian device conditions, including narrow phones, larger tablets and bright daylight. Check that a user can reach controls comfortably in a Sydney commuter setting and that text remains readable on budget devices commonly purchased through local carriers.
Test Navigation And Release Behaviour
Test each menu item, repeated taps, back navigation and process recreation. The selected destination should remain sensible after rotation, and the app should not crash if a fragment is temporarily removed from memory. Use Android Studio’s layout inspector and Logcat to identify incorrect IDs, missing resources and lifecycle errors.
Before publishing, test on several Android versions and screen sizes. Review the Google Play listing for Australian spelling, clear screenshots and prices in Australian dollars if the app sells digital products. A lightweight QA checklist can also help organise checks for documentation, content and release workflows when the tutorial project grows into a real product.
For a production app, use Espresso or Compose testing where appropriate, and verify that navigation works with TalkBack enabled. Confirm that deep links open the correct destination, saved content survives a restart and network errors do not leave a blank fragment.
Extend The Pattern Safely
Once the basic implementation works, you can replace manual fragment transactions with the Jetpack Navigation Component. A navigation graph defines destinations and actions, while BottomNavigationView can connect through NavigationUI. This approach reduces repeated code and supports deep links, animations and back-stack behaviour.
Fragments can then host a RecyclerView for local listings, a SQLite data layer for saved records or a dialog for user preferences. Keep data operations outside the fragment where possible, and expose loading, success and error states through a ViewModel.
A well-structured bottom navigation bar gives an Android app a predictable foundation. Whether the interface supports a Melbourne food directory, a Canberra study planner or a national shopping service, clear destinations, preserved state and accessible feedback make the experience easier to use.