0

I need your help. I have a tablayout with tintable imageview and textview. I want to highlight the default selected tab which is the first one. This is my sample code:

TabLayout.Tab tab;
mTabs = (TabLayout) findViewById(R.id.tabs);
mViewpager = (ViewPager) findViewById(R.id.viewpager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

mViewpager.setAdapter(pagerAdapter);
mViewpager.addOnPageChangeListener(new MyPageScrollListener(mTabs));
mTabs.setupWithViewPager(mViewpager);

// Iterate over all tabs and set the custom view
for (int i = 0; i < mTabs.getTabCount(); i++) {
    tab = mTabs.getTabAt(i);
    tab.setCustomView(pagerAdapter.getTabView(i));
}

mTabs.setOnTabSelectedListener(new MyOnTabSelectedListener());   
tab.select();

But it's selecting the last tab. Anyone here knows how to fix this? I'd appreciate any kind of help. Thanks!

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • This code doesn't compile. The `tab` is not within the scope, as it's being declared inside the `for-loop`. When you want to select a tab you should do `mViewpager.setCurrentItem(indexOfItem)` instead. – Darwind Nov 18 '15 at 13:10
  • @Darwind sorry I've updated my post. tried mViewpager.setCurrentItem(0) but it's not working –  Nov 18 '15 at 13:11

3 Answers3

1

Finally, I found a solution here: link

I added:

mTabs.getTabAt(1).select();    
mTabs.getTabAt(0).select();

And it works!

Community
  • 1
  • 1
1

I found a better solution. Instead if selecting the tab from the TabLayout you can do this with the ViewPager like so:

ViewPager viewPager = (ViewPager) findViewById(R.id.restaurant_menu_viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

// ViewPager mit TabLayout verknüpfen
TabLayout tabLayout = (TabLayout) findViewById(R.id.restaurant_menu_tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);

See the post here: https://stackoverflow.com/a/38762670/7318519

Chris8447
  • 306
  • 4
  • 7
0

You selected the last tab. Take a look at your source code :)

TabLayout.Tab tab;
...
for (int i = 0; i < mTabs.getTabCount(); i++) {
    tab = mTabs.getTabAt(i);
}
...
tab.select();

tab is the last tab you will get with getTabAt(int position).

So when you want to select the first tab this will help

maTabs.getTabAt(0).select();

Another (maybe better) solution is to turn the for loop into this

for (int i = mTabs.getTabCount() - 1; i >= 0; i--) {
        tab = mTabs.getTabAt(i);
}

Then your "last" tab you are getting in this loop is the first tab in your TabLayout.

An working full example code looks like this gist.

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • I followed this example: https://gist.github.com/tylerchesley/5d15d859be4f3ce31213 about the tintableImageview. But I even tried your updated code, but still (first tab) it's not highlighted. Whenever I click on other tabs, that's the only way to highlight the first tab. –  Nov 18 '15 at 13:46
  • I even tried setSelected(true) it highlights the 3 tabs but not the first one –  Nov 18 '15 at 14:24