Android Tab icon color is not clear
Asked Answered
P

1

1

I am trying to do color selector, when tab selected his color will be white, and when unselected - other color. For this goal I use setColorFilter. But tab icon color is not clear white when selected. Images were added with Image Asset and choosen custom color - white. I have to do it clear white. Any help, any idea appreciate.

enter image description here


This is my code

private int[] tabIcons = {
        R.drawable.ic_no,
        R.drawable.busket,
        R.drawable.ic_tab_aksia,
        R.drawable.ic_tab_menu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_icon_tabs);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(toolbar);

    frameLayout = (FrameLayout) findViewById(R.id.frameLayout);



    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(pageChangeListener);
    viewPager.setOffscreenPageLimit(10);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();


}


private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {


            FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
            fragmentToHide.onPauseFragment();

            FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(newPosition);
            fragmentToShow.onResumeFragment();



        currentPosition = newPosition;

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    public void onPageScrollStateChanged(int arg0) { }
};



private void setupTabIcons() {


        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);

        tabLayout.getTabAt(0).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#F0F0F0"), PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(2).getIcon().setColorFilter(Color.parseColor("#F0F0F0"), PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(3).getIcon().setColorFilter(Color.parseColor("#F0F0F0"), PorterDuff.Mode.SRC_IN);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.parseColor("#F0F0F0"), PorterDuff.Mode.SRC_IN);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
public void setCurrentItem (int item, boolean smoothScroll) {
        viewPager.setCurrentItem(item, smoothScroll);
    }
Perimorph answered 27/4, 2016 at 6:36 Comment(1)
Try to use the method mutate() on the icon drawable like this tab.getIcon().mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);. This could maybe solve your problem. :)Dyspeptic
B
0

Use SlidingTabLayout

siliding_tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
    @Override
    public int getIndicatorColor(int position) {
        return getResources().getColor(R.color.white);
    }               
});
Blane answered 27/4, 2016 at 6:45 Comment(1)
I don't need to the indicator color!Perimorph

© 2022 - 2024 — McMap. All rights reserved.