Android TabLayout select first Tab on Startup
Asked Answered
I

6

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

Inhumane answered 6/5, 2016 at 12:50 Comment(0)
I
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) {
    }

});
Inhumane answered 11/5, 2016 at 13:17 Comment(1)
setOnTabSelectedListener is deprecated. Use addOnTabSelectedListener instead.Paintbox
M
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();
    } 
Mandle answered 25/8, 2018 at 3:53 Comment(0)
I
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) {
    }

});
Inhumane answered 11/5, 2016 at 13:17 Comment(1)
setOnTabSelectedListener is deprecated. Use addOnTabSelectedListener instead.Paintbox
S
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)
            }
        }
Stillage answered 15/2, 2021 at 9:8 Comment(0)
F
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
  }
})
Falcate answered 13/3, 2018 at 13:53 Comment(0)
T
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

Toccaratoccata answered 26/1, 2023 at 2:13 Comment(0)
C
-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);
Cherisecherish answered 6/5, 2016 at 12:53 Comment(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. :(Inhumane
as @Inhumane mentioned, this won't work because the .select() will be ignored because startSelection == currentSelectedSmuggle

© 2022 - 2024 — McMap. All rights reserved.