74

I always see a crossed out line setOnTabSelectedListener for the following code

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }
}

And the error shown is:

setOnTabSelectedListener is deprecated

However, the program seems to work just fine. May I know what's going on?

Tim
  • 41,901
  • 18
  • 127
  • 145
gosulove
  • 1,645
  • 2
  • 26
  • 40

4 Answers4

213

May i know whats going on ?

Deprecated means that they decided this is not a good way to do it or they have found a better way of doing it, and this deprecated method will be removed in the future.

The fix is to use addOnTabSelectedListener instead of setOnTabSelectedListener.
It works almost the same way.

The difference is that with addOnTabSelectedListener:

  • you can add multiple listeners. With setOnTabSelectedListener you could only have one.
  • you should remove the listener when you're done with it, with removeOnTabSelectedListener.

General rule of thumb: never use deprecated methods.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • upvote for detailed answer covering all points, but I think 'fix is to use addOnTabSelectedListener' should be in bold to quickly find the answer – Shirish Herwade Apr 19 '17 at 06:14
  • 14
    @ShirishHerwade my goal is not to provide a quick answer, but to provide a complete answer. I think the explanation is as important as the solution :-) – Tim Apr 19 '17 at 08:35
  • 1
    Thank @Tim Castelijns it's helpfu but when am i supposed to use removeOnTabSelectedListener.? is it on onTabUnselected or onTabReselected? – eli Nov 15 '17 at 10:10
  • 2
    @eLi you use that when you no longer want to listen to tabSelected events. In practice, this is almost never required – Tim Nov 15 '17 at 10:54
21

As setOnTabSelectedListener is deprecated now, you can use the new method

addOnTabSelectedListener(OnTabSelectedListener)

This change allows to add multiple tab selection listeners to a single TabLayout.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
Chol
  • 2,097
  • 2
  • 16
  • 26
10

in kotlin usage like below code;

   tabLayoutView.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(p0: TabLayout.Tab?) {
                }

                override fun onTabUnselected(p0: TabLayout.Tab?) {

                }

                override fun onTabSelected(p0: TabLayout.Tab?) {
                    viewPager.currentItem = tabLayoutView.selectedTabPosition
                }


            })
Deniz
  • 319
  • 5
  • 9
5

Use below codes instead :

tablayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));

And if you want to sync them when clicking on tabs or swiping the viewpager above codes are the key for syncing.

Hadi Note
  • 1,386
  • 17
  • 16