gosulove gosulove - 1 year ago 597
Android Question

setOnTabSelectedListener is deprecated is prompted when creating tab layout

I copied some code from another website. The strange thing is that I always see this a crossed out line (

) for the following code

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabSelected(TabLayout.Tab tab) {

And the error shown is:

setOnTabSelectedListener is deprecated

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

Answer Source

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 the same way.

General rule of thumb: never use deprecated methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download