3

I was trying to make a tricky layout for which i need the Android's default tab indicator color.

I have searched a lot but every where I find how to change and customize tab indicator but could not find how to get color code in hex of default tab indicator.

Atif Rehman
  • 325
  • 1
  • 12

1 Answers1

1

I did some research for your question, I hope this will help you.

The tab indicator color is set in the Inner Class SlidingTabStrip of the class TabLayout (Code). Sadly you can't access this variable.

private class SlidingTabStrip extends LinearLayout {

    private final Paint mSelectedIndicatorPaint;

    // ...

    void setSelectedIndicatorColor(int color) {
        if (mSelectedIndicatorPaint.getColor() != color) {
            mSelectedIndicatorPaint.setColor(color);
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}

But in a constructor of the TabLayout the default tab indicator color is set.

public TabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    // Add the TabStrip
    mTabStrip = new SlidingTabStrip(context);
    addView(mTabStrip, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabLayout, defStyleAttr, R.style.Widget_Design_TabLayout);

    // <-- HERE
    mTabStrip.setSelectedIndicatorColor(a.getColor(R.styleable.TabLayout_tabIndicatorColor, 0));    
}

I think you need to access R.styleable.TabLayout_tabIndicatorColor to get what you want. I don't have the possibility right now to test if and how it works but I hope this helps you a bit.

Update

I tried this at home and it seems to work. I used this code in the onCreate() method of my Activity

TypedArray a = getContext().obtainStyledAttributes(null, R.styleable.TabLayout, 0, R.style.Widget_Design_TabLayout);
// returns -16738680 in my case which is the accentColor
int color = a.getColor(R.styleable.TabLayout_tabIndicatorColor, 0); 

But I saw, that R.styleable.TabLayout_tabIndicatorColor just links to the accentColor. Maybe this is the better way to get what you want.

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
    <item name="tabIndicatorColor">?attr/colorAccent</item>
    <!-- other items -->
</style>
Bona Fide
  • 714
  • 1
  • 9
  • 33