Change color indicator in SlidingTabLayout

I want to ask if it is possible to change the color of the tab indicator in SlidingTablayout? Should I use SlidingTabsColors from developer.android.com? I just want to change a different color instead of the default color (I think). Please advise. Thanks!!!

+6
source share
2 answers

Just to make it more clear.

SlidingTabLayout tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs); //referring the layout in xml file tabs.setViewPager(viewpager); //setting the viewpager //setting indicator and divider color tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return getResources().getColor(R.color.white); //define any color in xml resources and set it here, I have used white } @Override public int getDividerColor(int position) { return getResources().getColor(R.color.white); } }); 
+13
source

As you can see in the source code, you should implement the interface below

 /** * Allows complete control over the colors drawn in the tab layout. Set with * {@link #setCustomTabColorizer(TabColorizer)}. */ public interface TabColorizer { /** * @return return the color of the indicator used when {@code position} is selected. */ int getIndicatorColor(int position); /** * @return return the color of the divider drawn to the right of {@code position}. */ int getDividerColor(int position); } 

and install it by calling the method below from mSlidingTabLayout

 /** * Set the custom {@link TabColorizer} to be used. * * If you only require simple custmisation then you can use * {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)} to achieve * similar effects. */ public void setCustomTabColorizer(TabColorizer tabColorizer) { mTabStrip.setCustomTabColorizer(tabColorizer); } 

or you can just change

 private static final int DEFAULT_SELECTED_INDICATOR_COLOR = 0xFFF49e04; 

from the SlidingTabStrip class.

Edited by:

your main action or any objects that you want to control the color should implement the interface below:

 public class MainActivity extends FragmentActivity implements SlidingTabLayout.TabColorizer 

then in the override methods, select your color according to the position:

 @Override public int getIndicatorColor(int position) { return (Your color value ); } @Override public int getDividerColor(int position) { return (Your color value ); } 

Then you must pass this object to SlidingTab.

+2
source

Source: https://habr.com/ru/post/978887/


All Articles