Search code examples
androidandroid-tablayoutandroid-viewpager2

TabLayout has a background color when it shouldn't


I'm following this article on using ViewPager2 with a TabLayout as dots and it's mostly working but I have a small UI issue where the TabLayout has a background color that is washing out the unselected dots.

enter image description here

my activity UI:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="10dp">
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/intro_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/intro_tlPageIndicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:tabBackground="@drawable/pager_dot_selector"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp"
        app:tabPaddingStart="8dp"
        app:tabPaddingEnd="8dp" />
</FrameLayout>

drawable/pager_dot_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pager_selected_dot" android:state_selected="true" />
    <item android:drawable="@drawable/pager_default_dot" />
</selector>

drawable/pager_selected_dot

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="6dp"
            android:useLevel="false">
            <solid android:color="#8fa6b5"/>
        </shape>
    </item>
</layer-list>

drawable/pager_default_dot

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="6dp"
            android:useLevel="false">
            <solid android:color="#e8edf0"/>
        </shape>
    </item>
</layer-list>

style.xml:

<style name="App.Material" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:navigationBarColor">@color/primary</item>
    <item name="android:textColorPrimary">@color/textColor</item>
    <item name="android:textColorSecondary">@color/mutedTextColor</item>
    <item name="actionModeCloseDrawable">@drawable/close</item>
    <item name="actionModeStyle">@style/ActionModeStyle</item>
    <item name="actionMenuTextColor">#ffffff</item>
    <item name="colorAccent">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary</item>
    <item name="colorError">@color/error</item>
    <item name="colorPrimaryVariant">@color/primary</item>
    <item name="colorSecondaryVariant">@color/primary</item>
    <item name="colorSurface">@color/cardViewBackgroundColor</item>
    <item name="colorOnPrimary">#ffffff</item>
    <item name="colorOnSecondary">#ffffff</item>
    <item name="colorOnBackground">@color/textColor</item>
    <item name="colorOnError">#ffffff</item>
    <item name="colorOnSurface">@color/textColor</item>
    <item name="shapeAppearanceLargeComponent">@style/ShapeAppearance.LargeComponent</item>
    <item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.MediumComponent</item>
    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.SmallComponent</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="windowNoTitle">true</item>
</style>

Solution

  • @Zain helped me get on the right path to finding a solution and it appears the color I used for colorSurface is to blame, I took that out and my dots are now showing up as intended.