android.support.design.widget.TabLayout select Tab Programmatically
Asked Answered
M

8

24

I am using android.support.design.widget.TabLayout. It has two tabs, If user selects second tab On particular condition I want user to redirect to first tab and disallow him to go to sencond tab until condition matches. To achieve this I tried,

tabLayout.getTabAt(0).select(); 

but it does not reselect first tab

Milanmilanese answered 8/8, 2015 at 8:24 Comment(2)
Rather than allowing them to navigate and then having to redirect them, it may be easier to disable tab selection until the criteria are met: #4497687Gisela
@QuintinBalsdon , thanks, But example in your link is related to TabHost.It has setEnable() methode in tabhost.getTabwidget. I am using new TabLayout in support library.Milanmilanese
H
53

This is because that view is still not initialized properly, and you are trying to perform some action.

As a solution you just need to put one hadler before selecting perticular tab.

new Handler().postDelayed(
    new Runnable(){
        @Override
        public void run() {
            tabLayout.getTabAt(yourTabIndex).select();
        }
}, 100);
Hooghly answered 10/8, 2015 at 12:15 Comment(3)
is there any possibility to listen for an event (i.e. onCreationComplete) .. so that implement an event based solution, instead of using hard coded timer?Islamism
I guess there is no such method for that.Hooghly
I think I solved it without the hardcoded timer, see my answerOidea
F
3

This is how I solved it:

tabLayout.getTabAt(CurrentItem).getCustomView().setSelected(true);
Feodora answered 8/3, 2016 at 19:33 Comment(0)
V
2

This worked for me:

int tabIndex = 2;
tabLayout.setScrollPosition(tabIndex,0f,true);
viewPager.setCurrentItem(tabIndex);
Velazquez answered 23/12, 2017 at 9:52 Comment(0)
H
0

You can select the tab in Fragment.onViewCreated().

Hyacinthie answered 10/1, 2016 at 13:49 Comment(0)
M
0

This is my setup. works fine for me.

      //declare your tabs to be add on
        TabLayout tlDailyView;
        private TabLayout.Tab tabAppointment, tabSlots;


    @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_daily_view, container, false);
            initializeMembers();
            setupTabLayout();
            return view;
        }


    private void setupTabLayout() {
            tlDailyView.addTab(tabAppointment, 0, true);
            tlDailyView.addTab(tabSlots, 1, true);
            tlDailyView.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {

                    switch (tab.getPosition()) {
                        case 0:
                        //open fragment at position 0 here
                        case 1:
                        //open fragment at position 1 here
                            break;
                    }

                }

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

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        }

 private void initializeMembers() {
        tabSlots = tlDailyView.newTab();
        tabAppointment = tlDailyView.newTab();
        tabAppointment.setText(R.string.tab_appts).select();
        tabSlots.setText(R.string.tab_slots);
    }

don't forget to initialize your tab layout above.

Morass answered 12/4, 2017 at 10:43 Comment(0)
F
0
tabLayout.getTabAt(index specific).select();
Femmine answered 14/6, 2019 at 19:53 Comment(0)
N
0

It's too late but it might help other developers.

If you go inside and see how tabLayout.getTabAt(tabIndex).select(); has been implemented inside. You'll come to know that, it sends your flow to

onTabReselected(tab: TabLayout.Tab?)

and your flow doesn't go to

onTabSelected(tab: TabLayout.Tab?)

if your current tab and tabIndex are same. So this is very important to consider.

Nodab answered 11/10, 2019 at 13:27 Comment(0)
O
-1

Mihir's answer gave me an idea to try this. Seems like it works without the hardcoded timer, and also correctly updates the scroll for selected tab:

final TabLayout tabLayout = ...;
tabLayout.postOnAnimation(new Runnable() {
    @Override
    public void run() {
        tabLayout.getTabAt(2).select();
    }
});
Oidea answered 8/7, 2018 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.