Nirmal
Nirmal

Reputation: 939

Change switch button text color

I have successfully able to create a switch button in my application using the following custom class available in Github Switch Button Demo class

Here is the snapshot what i am getting Swith button

All is working good except one thing , I want to make text color to Black for unchecked state. I mean "OFF" text color is black when it is not selected. I tried to put some condition on my custom class but it not giving me the proper result. My style.xml file is like this

     <style name="mySwitchStyle">
          <item name="track">@drawable/switch_track_green</item>
          <item name="thumb">@drawable/background_fenced</item>
          <item name="switchTextAppearanceAttrib">@style/mySwitchTextAppearance</item>
          <item name="textOn">ON</item>
          <item name="textOff">OFF</item>
          <item name="pushStyle">false</item>
          <item name="textOnThumb">true</item>
          <item name="thumbExtraMovement">0dp</item>
          <item name="thumbTextPadding">6dp</item>
          <item name="trackTextPadding">6dp</item>
         <item name="switchMinWidth">69dp</item>
         <item name="switchMinHeight">36dp</item>
         <item name="switchPadding">6dp</item>
      </style>

      <style name="mySwitchTextAppearance">
          <item name="textColor">#FFeeFFee</item>
          <item name="textColorHighlight">#FFeeFFee</item>
          <item name="textColorHint">#FFeeFFee</item>
         <item name="textColorLink">#FFeeFFee</item>
         <item name="textSize">16sp</item>
         <item name="textStyle">normal</item>
      </style>

I tried to change the textColorHighlight, textColorHint, textColorLink to Black color but not giving me the desired result. Please help me to acheive the desired result.

Upvotes: 0

Views: 1895

Answers (1)

gio
gio

Reputation: 5020

You need to define textColor as color-list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:color="#ffffff"/> <!-- checked, white -->
    <item android:color="#000000"/> <!-- default, black -->
</selector>

Update:

Use SwitchCompact

SwitchCompat is a version of the Switch widget which on devices back to API v7. It does not make any attempt to use the platform provided widget on those devices which it is available normally.

Upvotes: 1

Related Questions