cgr
cgr

Reputation: 4636

Tab in the TabLayout (with ViewPager) is not visible when selected or swiped to right most page

I have implemented the tabs using new Android Design library (23.0.1 version). I followed this link http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/.
Everything is fine but: I have total 7 tabs so all tabs are not seen at the same time in the phone screen as the width of all tabs are more than the screen width. when I selected the right most tab (or any tab from right side), it does not come to the middle of the TabLayout (mid of the screen) so that the next hidden tabs on right side gets visibility.
Even when I swipe to right most page, it selects the tab with the indicator shown under the tab but the tab selected is off the screen. It should move to the middle of the screen.

Any clue ?

Upvotes: 0

Views: 950

Answers (2)

Kshitiz Mishra
Kshitiz Mishra

Reputation: 61

  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            tabLayout.setScrollPosition(position,positionOffset,true);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

using method addOnPageChangeListener worked for me

Upvotes: 0

cgr
cgr

Reputation: 4636

I had to add

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE) in source code

or

app:tabMode="scrollable" in XML.

Upvotes: 1

Related Questions