Search code examples
androidandroid-fragmentsandroid-tablayout

How can I switch between tabs in a TabLayout programatically from within a fragment


How can I switch between tabs in a TabLayout programatically from within a fragment which is currently displayed in the selected tab? I have a button in my fragment which I want to switch to the other tab\fragment, and I can not use - tabs.getTabAt(2).select(); because it does not work from inside the fragment.


Solution

  • I actually solved it now and it was quite easy. To switch tabs from within the fragment an access to the tabLayout is required and using tabs = getActivity().findViewById(R.id.tabLayout); worked just fine for that and then just call tabs.getTabAt(1).select(); like snachmsm suggested in the previous answer.