Change tabs text color in TabLayout to different colors programmatically

后端 未结 5 848
刺人心
刺人心 2021-01-18 14:02

I have 7 dates tabs in my screen, when tab selected, the text is black in color; while other select-able tabs are white in color. If the date falls on another month, I want

5条回答
  •  醉酒成梦
    2021-01-18 14:10

    Try this and let me know if this works for you:

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                if (tab.getPosition() == 0) {
                    tabLayout.getTabAt(0).getIcon().setAlpha(255);
                    tabLayout.getTabAt(1).getIcon().setAlpha(100);
                    tabLayout.getTabAt(2).getIcon().setAlpha(100);
                } else if (tab.getPosition() == 1) {
                    tabLayout.getTabAt(0).getIcon().setAlpha(100);
                    tabLayout.getTabAt(1).getIcon().setAlpha(255);
                    tabLayout.getTabAt(2).getIcon().setAlpha(100);
    
                } else if (tab.getPosition() == 2) {
                    tabLayout.getTabAt(0).getIcon().setAlpha(100);
                    tabLayout.getTabAt(1).getIcon().setAlpha(100);
                    tabLayout.getTabAt(2).getIcon().setAlpha(255);
    
                }
            }
    

    @Surya Prakash Kushawah your way is better.

提交回复
热议问题