Creating a Tabbed Interface with ViewPager2
ViewPager2 makes it straightforward to add swipeable pages to an Android app, while Material’s TabLayout provides a familiar navigation strip. Together, they support touch gestures, accessibility and keyboard navigation without requiring a separate activity for every section.
This implementation uses Kotlin and FragmentStateAdapter. Each page is a fragment that can manage its own data, retain useful state and follow standard Android lifecycle rules as the user moves between tabs.
Prepare the Android project
Add ViewPager2, the Material Components library and AndroidX Fragment support to the app module. Compatible stable versions should be checked against the rest of the project.
dependencies {
implementation("androidx.viewpager2:viewpager2:1.1.0")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.fragment:fragment-ktx:1.8.5")
}
Use one activity as the host and place each tab’s content in a fragment. This keeps navigation in a single Android component, makes state restoration more predictable and lets the pager handle swipe gestures efficiently.
Define reusable tab fragments
Treat the tab strip as a data-driven control. A small model separates titles from page creation and makes it easier to change the interface without rewriting the adapter.
private data class AppTab(val title: String)
private val appTabs = listOf(
AppTab("Overview"),
AppTab("Activity"),
AppTab("Saved")
)
FragmentStateAdapter is a practical choice for ViewPager2 because it creates fragments lazily and preserves their saved state when pages leave the viewport. The adapter can create a generic fragment and pass the selected title or filter through its arguments.
private class TabAdapter(
activity: FragmentActivity,
private val tabs: List<AppTab>
) : FragmentStateAdapter(activity) {
override fun getItemCount() = tabs.size
override fun createFragment(position: Int): Fragment =
TabFragment.newInstance(tabs[position].title)
}
Lay out the tab strip and pager
Place a TabLayout above a ViewPager2 in the activity layout. The pager receives the remaining vertical space, allowing each fragment to use the full content area.
<LinearLayout
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:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
After setting the adapter, connect the controls with TabLayoutMediator. The mediator creates the tabs, applies their labels and keeps the selected page synchronised with the visible fragment.
val pager = findViewById<ViewPager2>(R.id.viewPager)
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
pager.adapter = TabAdapter(this, appTabs)
TabLayoutMediator(tabLayout, pager) { tab, position ->
tab.text = appTabs[position].title
}.attach()
Preserve state between tab changes
FragmentStateAdapter and ViewPager2 work with the fragment lifecycle to retain page state. A fragment’s view may be released while its page is off-screen, but saved instance state can restore scroll positions, entered text and other relevant details. Data shared by several tabs is better placed in an activity-scoped ViewModel.
Do not replace the adapter or reset the current item every time the activity resumes, as this can discard restored state. Keep the tab order and identifiers consistent, and use SavedStateHandle when information must survive process death rather than only a configuration change.
Add interaction and accessibility
Use TabLayoutMediator instead of manually selecting tabs from every page-change callback. The mediator already synchronises the strip and pager, so duplicate selection logic can create unnecessary feedback loops. If a selected tab should return to the top of a list, handle tab re-selection separately and only scroll when that action is appropriate.
Give every tab a meaningful text label rather than names such as “Tab One”. When an icon is included, provide an accessible description and keep the text label available for TalkBack. Material components supply useful defaults, but font scaling, minimum touch targets and focus order should still be tested with large display sizes.
Each page should also present clear loading, empty and error states. Keeping this feedback inside the relevant fragment prevents one slow data source from making the entire interface appear frozen. Cached values can remain visible while a refresh takes place, with a small progress indicator showing that newer information is being loaded.
Scheduled updates should not run on the UI thread. If tab content needs periodic background synchronisation, the site’s periodic WorkManager guidance explains how to defer that work and apply network constraints suitable for mobile connections.
Design for Australian Android users
Commuters in Sydney and Melbourne often check train, tram or bus timetables while waiting at a station or platform. A tabbed interface must remain readable on smaller screens and usable with one hand, especially when someone is moving between a platform, office and home without time to explore a complex menu.
The Australian app market includes banking, supermarket, utility, retail and transport services, with Android used across Samsung devices, Google Pixel phones and lower-cost handsets sold by local retailers and carriers. Users may move between these services while managing limited mobile data, so cached content and efficient loading are valuable. Regional travel can also involve intermittent reception, making offline states and retry controls important.
Privacy requirements must be considered alongside the interface design. If an app falls under the Privacy Act 1988 and the Australian Privacy Principles, it should explain the purpose of collected information, avoid collecting unrelated data and provide appropriate handling of personal details. A tab that displays personalised content should make that purpose clear before asking users to share account, location or behavioural information.
Recommendations for a reliable tabbed flow
The following practices produce a tabbed interface that behaves consistently across devices and everyday use:
- Keep tab labels, ordering and identifiers stable between launches.
- Use FragmentStateAdapter and retain shared data in a ViewModel.
- Provide loading, empty and error states within every page.
- Preserve scroll positions and form values when users switch tabs.
- Support large text, TalkBack and devices with limited screen space.
- Test rotation, process restoration, swipe gestures and repeated network requests.
Together, these choices keep navigation predictable while allowing each page to operate like a normal Android screen with its own data, lifecycle and accessibility requirements.