9

I'm using TabLayout from Android Design Library. I have multiple tabs and each Tab has an action when it is selected. So I have an attribute startSelection, which performs

tabLayout.getTabAt(startSelection).select();

This selects the tab and performs the action for this tab. It works fine for each Tab except the first one, which is automatically selected on Startup without (!) performing the action. Does anyone have a solution for this?

I don't want to use the onTabReselected method, because this causes another behaviour of the TabLayout. Also selecting the second tab and selecting the first tab afterwards is no good solution.

Best regards

6 Answers 6

6

I had a similar problem with a custom tab layout I was implementing, when starting the activity the first tab wouldn't appear in the selected state but tab 2,3,4... would when auto-selected on startup.

The solution that helped me was in onResume(), quickly select the second tab then return to the first tab.

    @Override
    protected void onResume() {
        super.onResume();
        mTabLayout.getTabAt(1).select();
        mTabLayout.getTabAt(0).select();
    } 
5

I got it. The solution is simple, use (once) onTabReselected and overwrite listener there.

tabLayout.setOnTabSelectedListener(new OnTabSelectedListener() {

    @Override
    public void onTabSelected(Tab tab) {
        selectTab(tab);
    }

    private void selectTab(Tab tab) {
        // do something                 
    }

    @Override
    public void onTabReselected(Tab tab) {
        if (tab.getPosition() == 0) {
            selectTab(tab);

            tabLayout.setOnTabSelectedListener(new OnTabSelectedListener() {

                @Override
                public void onTabSelected(Tab tab) {
                    selectTab(tab);
                }

                @Override
                public void onTabReselected(Tab arg0) {                             
                }

                @Override
                public void onTabUnselected(Tab arg0) {                             
                }
            });

        }
    }


    @Override
    public void onTabUnselected(Tab tab) {
    }

});
1
  • 1
    setOnTabSelectedListener is deprecated. Use addOnTabSelectedListener instead. Commented Oct 1, 2019 at 15:49
3

With use of adding custom tabs also can achieve this TabLayout's addTab() method with 2 arguments e.g

//first args tab //second args setSelected

tabLayout.addTab(tab, true/false)

//example

for (item in tabsList) {
            var tab = tabLayout.newTab()
            tab.text = item.name
            if (conditions for selections) {
                tabLayout.addTab(tab, true)
            } else {
                tabLayout.addTab(tab, false)
            }
        }
0

i have more simple solution for that

tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
  private var alreadyReselected = AtomicBoolean(false)

  override fun onTabReselected(tab: TabLayout.Tab) {
    if  (tab.position == 0 && !alreadyReselected.getAndSet(true)) onTabSelected(tab)
  }
  override fun onTabUnselected(tab: TabLayout.Tab?) {}

  override fun onTabSelected(tab: TabLayout.Tab) {
    //do whatever you want on first selection
  }
})
0

you just need reverse the logic like this

setup onTabSelected first, after fragment knows which tab selected, setup:

tabLayout.addTab(tabLayout.newTab().setText("Duration"), true)

second param to true if the added tab should become the selected tab

-2

When you are trying to select first tab grammatically that time your view initialization is not completed. Use handle and wait till 100ms then try to select tab.

Try this in your onCreate()

new Handler().postDelayed(
    new Runnable(){
        @Override
        public void run() {
            tabLayout.getTabAt(startSelection).select();
        }
}, 100);
2
  • 2
    Hi, the initialization process is finished when I do this. As i wrote, selecting a tab works for tab 2,3,4, and so on. But not for the first one, because that tab is selected by default. Selecting a selected tab does not perform any action. :( Commented May 6, 2016 at 13:40
  • as @user2331454 mentioned, this won't work because the .select() will be ignored because startSelection == currentSelected
    – Bugdr0id
    Commented Sep 2, 2016 at 14:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.