Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TabLayout setOnTabSelectedListener not working

Let's say I have MainActivity class where the TabLayout and ViewPager are initialized with corresponding 4 pages.

MainActivity.class

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            // return PlaceholderFragment.newInstance(position + 1);
            switch (position) {
                case 0:
                    ActivityZero tab0 = new ActivityZero();
                    return tab0;
                case 1:
                    ActivityOne tab1 = new ActivityOne();
                    return tab1;
                case 2:
                    ActivityTwo tab2 = new ActivityTwo();
                    return tab2;
                case 3:
                    ActivityThree tab3 = new ActivityThree();
                    return tab3;
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }
        //Setting the ICON
        private int[] imageResId = {
                R.drawable.ic_zero
                R.drawable.ic_one,
                R.drawable.ic_two,
                R.drawable.ic_three
        };
        @Override
        public CharSequence getPageTitle(int position) {
            Drawable image = ContextCompat.getDrawable(getApplicationContext(), imageResId[position]);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            SpannableString sb = new SpannableString(" ");
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
        }
    }
}

Everything is working fine with the above code, but when I add setOnTabSelectedListener in onCreate with this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);

        //This is what i'm trying to add
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setAlpha(255);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setAlpha(127);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

When I run, everything's work fine, but when I swipe to change the tab, the app is no longer responding and start to crash.

I'm trying to get the selected tab position and set the icon with setAlpha(127) on the selected page, but no luck.

NOTE: I'm using support library and my current API is 8.

UPDATE:

12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/InputEventReceiver: Exception dispatching input event.
12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: java.lang.NullPointerException
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at dalvik.system.NativeStart.main(Native Method)
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp D/AndroidRuntime: Shutting down VM
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4131e2a0)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: FATAL EXCEPTION: main
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: java.lang.NullPointerException
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

Someone can help me here? Thank you very much.

like image 741
bernzkie Avatar asked Sep 27 '22 01:09

bernzkie


1 Answers

Okay I think I understand now, you are using the method getIcon() but you never actually set an icon via setIcon() so you are returning null on getIcon(). You are instead using the getPageTitle() to display the icon so the tab has no idea there even is an icon.

What I would recommend doing is modify getPageTitle() to always return null and then create a tab setup method setupTabs() in your main activity class which should look something like:

private void setupTabs() {
    mTabLayout.getTabAt(0).setIcon(mTabIcons[0]);
    mTabLayout.getTabAt(1).setIcon(mTabIcons[1]);
    mTabLayout.getTabAt(2).setIcon(mTabIcons[2]);
    mTabLayout.getTabAt(2).setIcon(mTabIcons[3]);
}

Also move your imageResId array from the adapter out to the activity:

private int[] mTabIcons = {
    R.drawable.ic_zero
    R.drawable.ic_one,
    R.drawable.ic_two,
    R.drawable.ic_three
};

You should also change your setOnTabSelectedListener to use TabLayout.ViewPagerOnTabSelectedListener so that keeping the tabs in sync is handled for you:

    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            tab.getIcon().setAlpha(255);
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);
            tab.getIcon().setAlpha(127);
        }
    });

Then you can call setupTabs() in your onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // TabLayout
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(mViewPager);

    // Set up our tab icons
    setupTabs();

    //This is what i'm trying to add
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            tab.getIcon().setAlpha(255);
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);
            tab.getIcon().setAlpha(127);
        }
    });
}
like image 100
cziemba Avatar answered Oct 20 '22 11:10

cziemba