TabLayout tab selection
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
TabLayout from the Material Components library lets you create horizontal tabs in Android. To programmatically select a tab, call tabLayout.getTabAt(index).select(). To respond to selection changes, add an OnTabSelectedListener. TabLayout is typically paired with ViewPager2 to synchronize tab selection with page swiping using TabLayoutMediator. The key distinction is between user-driven selection (taps and swipes) and programmatic selection (your code calling select()).
Basic TabLayout Setup
Tab Selection Listener
TabLayout with ViewPager2
TabLayoutMediator handles bidirectional sync — tapping a tab scrolls the pager, and swiping the pager updates the selected tab.
Custom Tab Views
Programmatic Tab Selection
Styling Tabs
Common Pitfalls
- Calling
getTabAt(index).select()before tabs are added: If you select a tab inonCreatebefore the ViewPager adapter is set andTabLayoutMediator.attach()is called, the selection is overridden. Callselect()afterattach()or usetabLayout.post { ... }to defer the selection. - Listener firing during programmatic selection:
onTabSelectedfires both for user taps and programmaticselect()calls. If your listener triggers network requests or navigation, it runs unexpectedly during setup. Use a boolean flag or remove/re-add the listener around programmatic selection. - Using ViewPager (1) instead of ViewPager2: The original
ViewPagerusesTabLayout.setupWithViewPager(), whileViewPager2usesTabLayoutMediator. Mixing them up (e.g., callingsetupWithViewPagerwith a ViewPager2) causes compilation errors or runtime crashes. Use the API that matches your pager. - Tab indicator not showing on scrollable TabLayout: When
tabMode="scrollable"is set and there are few tabs, the indicator may appear misaligned or missing. EnsuretabGravityis set to"start"(not"fill") with scrollable mode, and verify that tab text is not empty — empty tabs have zero width. - Memory leaks from OnTabSelectedListener in Fragments: Adding a listener in
onViewCreatedwithout removing it inonDestroyViewcauses the fragment to leak. Store the listener in a variable and calltabLayout.removeOnTabSelectedListener(listener)inonDestroyView.
Summary
- Use
tabLayout.getTabAt(index)?.select()to programmatically select a tab - Add
OnTabSelectedListenerto respond to tab changes from both taps and programmatic selection - Pair TabLayout with ViewPager2 using
TabLayoutMediatorfor swipe-to-tab synchronization - Use
tabLayout.post { }to defer programmatic selection until layout is complete - Remove tab listeners in
onDestroyViewto prevent memory leaks in fragments
Related reading
- Table Header Views in StoryBoards
- Tableview scroll content when keyboard shows
- TableView slow when adding images to cellForRowAtIndex
- Take iOS Simulator screenshots including device frame?
- Take screenshots in the iOS simulator
- Tap Action not working when Color is clear SwiftUI
- tap for more information or stop the app
- targetContentOffsetForProposedContentOffsetwithScrollingVelocity without subclassing UICollectionViewFlowLayout
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.