How to use selectors to change icons with the new TabLayout
Asked Answered
C

5

18

I'm using the new support TabLayout from Android. The thing is that I wanted to use selectors to change the icon when a tab is selected.

I've been looking into the source code and it seems to me that the it never changes the state of the view (and for that reason I can't use the selector).

Does anyone knows some workaround?

Thank you!

Cremona answered 22/6, 2015 at 9:35 Comment(1)
how u resolved ur issue because mine is not getting resolved from customViewAriel
U
0

There is way to set customView as a tab with setCustomView(View view) method. So you can create a textview and set a selector to it and set this view to tab.

Hope it helps you!

Urion answered 24/6, 2015 at 6:42 Comment(0)
N
59

Assume your my_selector.xml is,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_on" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_off"/> <!-- default -->
</selector>

then you can call setIcon directly,

tab.setIcon(R.drawable.my_selector);

Verified with 'com.android.support:design:22.2.0'.

Noma answered 12/8, 2015 at 4:5 Comment(2)
This works great but I've encountered a quirk. The first tab doesn't get selected. Is it only me? I've tried manually setting the pager to 0 (pager.setCurrentItem(0);)Danuloff
@kgandroid, were you looking at android.support.design.widget.TabLayout.Tab? It has the method.Noma
D
1

I found that when I first set the custom view for each tab in the TabLayout I need to set the first one (index 0) as selected.

    TabLayout toolbarTabLayout = (TabLayout) findViewById(R.id.tabs);
    toolbarTabLayout.setupWithViewPager(mViewPager);
    toolbarTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    toolbarTabLayout.setTabMode(TabLayout.MODE_FIXED);
    toolbarTabLayout.setTabTextColors(R.color.colorPrimary, R.color.white);
    // Iterate over all tabs and set the custom view
    for (int i = 0; i < toolbarTabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = toolbarTabLayout.getTabAt(i);
        View v=mSectionsPagerAdapter.getTabView(i);
        // no tabs are actually selected at start, this will make sure the
        // selector for the colors comes in right when initialized
        if (i==0)
            v.setSelected(true);
        tab.setCustomView(v);
    }

This seems to force the first tab as selected when the custom view is applied. It really feels like a hack, hopefully someone else will figure out the real issue and propose a better fix.

Doubtless answered 11/11, 2015 at 2:24 Comment(0)
U
0

There is way to set customView as a tab with setCustomView(View view) method. So you can create a textview and set a selector to it and set this view to tab.

Hope it helps you!

Urion answered 24/6, 2015 at 6:42 Comment(0)
P
0

If you did everything right (and i believe this) so you arrived at same point than me. Maybe it is a little bug in new android appcompat library.

i found an workaround (it's called Gambiarra in a good Portugues) to solve this problem. you need to call the method select() from Tab class like this:

mTabLayout.getTabAt(x).select();

BUT it's very important: x variable must be different than current selected tab index.

Punctuation answered 28/7, 2015 at 19:51 Comment(0)
N
0

This is what worked for me:

Assuming you have your selectors set in the drawable res folder (like Xingang Huang showed above). In your MainActivity (where you setup your TabLayout) you include your array of icon selectors and then you loop through it like this:

for (int i = 0; i < yourTabLayout.getTabCount(); i++) {
        ImageView imageView = new ImageView(this); //your context, in this case MainActivity.class
        imageView.setImageResource(arr_tabIcons[i]); //tabIcons is the array of icons
        if (i==0) {
            imageView.setSelected(true); 
        }
        yourTabLayout.getTabAt(i).setCustomView(imageView);

    }

tab.setIcon(R.drawable.icon)

works as well but in my case the icons looked really small, so I had to use the solution with the ImageView to fill the tab view.

Happy coding ;)

Negativism answered 28/5, 2017 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.