Java – Android: Tracking switchcompat is invisible against a white background

Android: Tracking switchcompat is invisible against a white background… here is a solution to the problem.

Android: Tracking switchcompat is invisible against a white background

I

have an activity with a white background, I added a switchcompat, but the track is not visible when the switch is not activated (off). All that is visible is the thumb, so the user simply cannot see that it is a switch. How do I make the track of the switch visible when it is in a non-active position?

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/someswitchname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        />

Solution

The way to do this is to define a custom theme like this:

<style name="Custom.SwitchCompat.WhiteTrack" parent="Base.Widget.AppCompat.CompoundButton.Switch">
        <!-- Inactive track color(30% transparency) -->
        <item name="android:colorForeground">[some color]</item>
    </style>

Then define the theme in the layout like this:

android:theme="@style/Custom.SwitchCompat.WhiteTrack"

And voila.

Related Problems and Solutions