0

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

Janvi Vyas
  • 732
  • 5
  • 16
ajay dhadhal
  • 306
  • 4
  • 16

3 Answers3

2

you can do it by using your xml file.just add this in your view...

app:tabBackground="@drawable/tab_selector_reg" //for tab layout

or you can use android:background=""

tab_selector_reg file

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_selected" android:state_selected="true"/>
<item android:drawable="@color/water_app_yellow_transparent"/> </selector>
Mohammad Adil
  • 503
  • 6
  • 13
2

Use below code for selection:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            if (i == position) {
                tabLayout.getTabAt(i).getCustomView().setBackgroundColor(getResources().getColor(R.color.selected_tab_color));
            } else {
                tabLayout.getTabAt(i).getCustomView().setBackgroundColor(getResources().getColor(R.color.unselected_tab_color));
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});
Mahesh Keshvala
  • 1,349
  • 12
  • 19
1

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);                
            }
        }
    });
ajay dhadhal
  • 306
  • 4
  • 16