Cannot remove PagerTabStrip tab indicator line - Android

1.9k Views Asked by At

I would like to remove the PagerTabStrip tab indicator, but I can't find a way. I was trying to set setDrawFullUnderline(false) but it is still there. If I set its color to transparent, I get a black line :-/

How could I get rid of it? Thanks!

PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip); pagerTabStrip.setDrawFullUnderline(true); pagerTabStrip.setTabIndicatorColor(Color.TRANSPARENT);

<android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="390dp"
        android:layout_above="@+id/page_indicator"
        android:padding="20dp">

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_tab_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@android:color/transparent"
            android:textAppearance="@style/PagerTabStripText"
            android:paddingTop="5dp"
            android:paddingBottom="5dp" />

        </android.support.v4.view.ViewPager>

enter image description here

2

There are 2 best solutions below

0
On

Yeah, the docs for

setTabIndicatorColor()

say:

The high byte (alpha) is ignored.

sets its own alpha on the tab strip depending on the position as you’re sliding the page.

So let’s be evil and stomp all over that!

public class PagerTabStripNoInd extends PagerTabStrip {
    private static final String TAG = "PagerTabStripNoInd";
    private static final Integer ZERO = new Integer(0);
    public PagerTabStripNoInd(Context context) {
        super(context);
    }
    public PagerTabStripNoInd(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        try {
            Field tabAlphaField = PagerTabStrip.class.getField("mTabAlpha");
            tabAlphaField.setAccessible(true);
            tabAlphaField.set(this, ZERO);
        } catch (NoSuchFieldException e) {
            Log.e(TAG, "onDraw", e);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "onDraw", e);
        }
        super.onDraw(canvas);
    }
}

Since the position-dependent alpha is calculated in a different method, if we set it to zero just before we draw, the tab color ends up really being transparent.

Use it like this:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="390dp"
    android:layout_above="@+id/page_indicator"
    android:padding="20dp">

    <!-- use your fully qualified class name here, for example -->
    <com.example.PagerTabStripNoInd
        android:id="@+id/pager_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@android:color/transparent"
        android:textAppearance="@style/PagerTabStripText"
        android:paddingTop="5dp"
        android:paddingBottom="5dp" />

</android.support.v4.view.ViewPager>
0
On
    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setIndicatorHeight(0);

Set height of indicator zero. I hope it will help.