How to Create Fragment Using Bottom Navigation in Social Media Android App
In this article
To create fragments with bottom navigation in a social media Android app, you set up a single activity that hosts a BottomNavigationView and a FragmentContainerView, then swap fragments when a tab is selected. The shortest working set of steps is: add the Material Components dependency, create a menu resource for the tabs, build one fragment class per tab, wire the navigation view to replace the container's fragment, and handle state so the selected tab survives rotation. This guide walks through each step using the standard AndroidX and Material libraries, with Kotlin examples that work in a new or existing project.
What You'll Need
- Android Studio with a project targeting a recent Android API level (the exact minimum varies by library version; check the Material Components release notes for the current requirement).
- The AndroidX Fragment and Material Components libraries added to your app-level
build.gradlefile. The Material library providesBottomNavigationView. - One activity to act as the host. Most social apps use a single
MainActivityfor the logged-in area. - Basic familiarity with XML layouts and Kotlin (or Java). The steps below use Kotlin, but the same APIs exist in Java.
Step-by-Step
- Add the dependencies. In your app-level
build.gradle(orbuild.gradle.kts), make sure you have the Material Components and Fragment KTX libraries. Sync the project after adding them. If you already created the project with a Bottom Navigation Activity template, these are included and you can skip to step 3. - Create the menu resource for the tabs. In
res/menu, create a file such asbottom_nav_menu.xml. Add one<item>per tab, each with a uniqueandroid:id, an icon, and a title. For a social app, typical tabs are Home (feed), Search, Notifications, and Profile. Keep titles short so they do not wrap on small screens. - Build the host layout. In your main activity layout, place a
FragmentContainerViewwith an id such asfragment_containerabove aBottomNavigationViewwith an id such asbottom_navigation. Setapp:menu="@menu/bottom_nav_menu"on the navigation view. Use a verticalLinearLayoutor aConstraintLayoutso the navigation bar sits at the bottom and the container fills the remaining space. - Create one fragment per tab. For each menu item, create a fragment class (for example
HomeFragment,SearchFragment,NotificationsFragment,ProfileFragment). Each fragment should have its own layout file. Keep fragments independent: they should not directly reference each other. If one tab needs to open another, navigate through the host activity or a shared ViewModel. - Wire the navigation view in the activity. In
MainActivity, set a listener on theBottomNavigationView. When an item is selected, replace the fragment in the container using the fragment manager. A simple pattern is to check the selected item id and callsupportFragmentManager.beginTransaction().replace(R.id.fragment_container, SelectedFragment()).commit(). Set the default selected item to Home inonCreateso the app opens on the feed. - Preserve the selected tab across rotation and process death. If you always replace the fragment on selection, the tab resets after rotation. Save the selected item id in
onSaveInstanceStateand restore it inonCreate. Alternatively, use the Navigation component with a nav graph andsetupWithNavController, which handles back stack and state for you. Choose one approach and stay consistent; mixing manual fragment transactions with the Navigation component causes confusing back behavior. - Handle back navigation. In a social app, pressing back from a non-home tab should usually return to Home before exiting. With manual transactions, override
onBackPressed(or useOnBackPressedDispatcher) to check whether the current tab is Home; if not, select Home. With the Navigation component, configure the start destination and let the library manage the back stack. - Test on multiple screen sizes and both orientations. Run the app on a small phone and a tablet emulator. Confirm that the bottom navigation does not overlap content, that tab labels are readable, and that the selected fragment is restored after rotating the device. Also test with the system back gesture on gesture-navigation devices.
Common Mistakes
- Using
FragmentContainerViewwith aFrameLayoutid mismatch. The id in the transaction must match the id in the layout exactly. A typo causes a runtime crash or a blank screen. - Replacing fragments on every selection. This destroys and recreates fragments, losing scroll position and form input. Use
show/hidewithadd, or the Navigation component, if you need to preserve state. - Forgetting to set the initial tab. If you do not select an item in
onCreate, the container stays empty until the user taps a tab. - Putting business logic in the activity. The host activity should only manage navigation. Each fragment should own its own data loading and UI logic.
- Ignoring configuration changes. Without saved state, the selected tab resets on rotation, which feels broken to users.
- Overloading the bottom bar. Material guidelines recommend three to five items. More than five items crowd the bar and make labels unreadable.
Tips
- Use the Material 3 theme so
BottomNavigationViewpicks up the correct colors and ripple behavior automatically. - Keep tab icons simple and filled when selected, outlined when not, to match platform conventions.
- If your social app has a floating action button for creating posts, place it above the bottom navigation and check that it does not cover tab labels.
- Use a shared
ViewModelscoped to the activity if two tabs need the same data, such as the current user profile. - Prefer the Navigation component for new projects: it reduces manual fragment transactions and handles deep links and back stack consistently.
Troubleshooting
If the app crashes on launch, check the logcat for an InflateException or a ClassNotFoundException. The most common cause is a missing dependency or a wrong fragment class name in the layout. If the bottom navigation appears but tapping a tab does nothing, confirm that the listener is set and that the fragment container id in the transaction matches the layout. If the selected tab resets after rotation, add saved-state handling as described in step 6. If the fragment content is hidden behind the navigation bar, add bottom padding to the fragment layout or use android:fitsSystemWindows="true" on the root layout. If back navigation exits the app from a non-home tab, implement the back handling in step 7. If the app works on one device but not another, check the Material library version and the device's Android version; labels and menu behavior can differ slightly by version and region.
Frequently Asked Questions
Do I need the Navigation component to use bottom navigation?
No. You can use BottomNavigationView with manual fragment transactions. The Navigation component is optional but simplifies back stack and state handling.
How many tabs should a social media app have?
Material guidelines recommend three to five. Common choices are Home, Search, Notifications, and Profile.
Why does my fragment disappear after rotating the device?
The activity is recreated and the fragment transaction is not automatically restored if you replace fragments manually. Save the selected item id and restore it in onCreate, or use the Navigation component.
Can I use the same fragment for two tabs?
Yes, but each tab should represent a distinct destination. Reusing one fragment class with different arguments is fine; reusing the same instance in two tabs is not recommended because state and lifecycle become confusing.
Does bottom navigation work on tablets and foldables?
Yes, but consider a navigation rail on larger screens for better ergonomics. The bottom bar remains functional on tablets.
What is the difference between FragmentContainerView and FrameLayout for the host?
FragmentContainerView is designed for fragments and handles some animation and lifecycle edge cases better. Use it for new layouts.