How to get current selected tab index in TabLayout?
Asked Answered
P

5

40

When I use ActionBar tabs, I use this code.

private int getCurrentTabIndex() {
    ActionBar actionBar = activity.getSupportActionBar();
    ActionBar.Tab selectedTab = actionBar.getSelectedTab();
    if(selectedTab == null){
        return 0;
    }

    return selectedTab.getPosition();
}

But how can I do it using TabLayout?

Paestum answered 1/7, 2015 at 22:26 Comment(1)
int tab_position=tabLayout.getSelectedTabPosition(); same can be achieved without overriding ,I have tried it with targetSDK=24Rustic
C
82

Use OnTabSelectedListener.

And then in this listener get the getPosition().

Something like this:

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
    @Override
    public void onTabSelected(TabLayout.Tab tab){
        int position = tab.getPosition();
    }
});

UPDATE

This method setOnTabSelectedListener() is deprecated . Use addOnTabSelectedListener(OnTabSelectedListener)

Centrosymmetric answered 1/7, 2015 at 23:5 Comment(4)
Note that if you are using setupWithViewPager() you should extend TabLayout.ViewPagerOnTabSelectedListener instead of TabLayout.OnTabSelectedListener and make sure to call it after setupWithViewPager() if you want to default integration with ViewPager functionality.Tui
setOnTabSelectedListener() is now deprecated.Dorolisa
This method setOnTabSelectedListener() is deprecated . Use addOnTabSelectedListener(OnTabSelectedListener) instead.Rodney
onTabSelected is called with default value 0 after recreation, how can I save the last selected tab?Gills
C
38

setOnTabSelectedListener is now deprecated. you can use addOnTabSelectedListener instead. To remove the listener you can use removeOnTabSelectedListener

 mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
Centistere answered 29/6, 2016 at 8:19 Comment(0)
R
33

You can simply do it by calling getSelectedTabPosition() on your TabLayout instance like:

int tab_position=tabLayout.getSelectedTabPosition();

and in Kotlin

val tabPosition = mTabLayout.selectedTabPosition
Rustic answered 8/7, 2016 at 14:58 Comment(1)
if you want the actual Tab tabLayout.getTabAt(layout.selectedTabPosition)Priapic
A
0

if you are using tabs with viewpager then the asked task could be done as follows:

use the following code after tabLayout.setUpWithViewPager(viewPager)

tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
        switch(tab.getPosition()){
            // do something here
        }
        super.onTabSelected(tab);
    }
});
Allocution answered 22/7, 2016 at 16:47 Comment(1)
setOnTabSelectedListener is now deprecated, you can use addOnTabSelectedListener. see reply of Doron https://mcmap.net/q/389499/-how-to-get-current-selected-tab-index-in-tablayoutSergiosergipe
V
0

If you are using TabLayout inside ViewPager (and initializing it with setupWithViewPager()), you can do it using ViewPager instance.

    View view = inflater.inflate(R.layout.fragment_view_pager_tabs, container, false);
    ViewPager viewPager = view.findViewById(R.id.view_pager_tabs);
    viewPager.setAdapter(pagerAdapter);
    TabLayout tabLayout = viewPager.findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);
    int item = viewPager.getCurrentItem(); // get
    viewPager.setCurrentItem(item, true);  // set
Varus answered 13/9, 2018 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.