Search code examples
androidtextviewandroid-tablayout

change Custom Tab layout Textview background color


I want to Change Background Color of custom Tab. I have Textview in Custom Tab.I tried Following code But Textveiw background color not changed.

    final TextView tabText_customtab1=(TextView)findViewById(R.id.tabText_customtab1);
    final TextView tabText_customtab2=(TextView)findViewById(R.id.tabText_customtab2);   

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
    {
        @Override
        public void onTabSelected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundColor(getResources().getColor(R.color.selected_tab_color));
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab2.setBackgroundColor(getResources().getColor(R.color.unselected_tab_color));                  
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab)
        {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab)
        {
            if(tab.getPosition()==0)
            {
                tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
            }
            else if(tab.getPosition()==1)
            {
                tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);               
            }
        }
    });

Thanks in Advance


Solution

  • There is small Mistack by me.The Right is Following.

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
        {
            @Override
            public void onTabSelected(TabLayout.Tab tab)
            {
                if(tab.getPosition()==0)
                {
                    tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
                    tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
                }
                else if(tab.getPosition()==1)
                {
                    tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
                    tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);
    
                }
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab)
            {
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab)
            {
                if(tab.getPosition()==0)
                {
                    tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
                    tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
                }
                else if(tab.getPosition()==1)
                {
                    tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
                    tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);                
                }
            }
        });