Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change colour indicator in SlidingTabLayout

Tags:

android

I wish to ask that is it possible to change the colour of the tab indicator in the SlidingTablayout? Must I use SlidingTabsColors from developer.android.com? I just want to change another colour instead of the default blue(I think) colour. Please advise. Thanks!!!

like image 200
DanKCl Avatar asked Nov 30 '14 05:11

DanKCl


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);
    }
});
like image 109
oneavi Avatar answered Oct 23 '22 21:10

oneavi


As you can see in source code you must implement below interface

/**
 * 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 set it by calling below method 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 SlidingTabStrip class.

Edited:

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

public class MainActivity extends FragmentActivity implements SlidingTabLayout.TabColorizer

then in the override methods select your color according to 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 that object to SlidingTab.

like image 30
mmlooloo Avatar answered Oct 23 '22 22:10

mmlooloo