'Change switch color programmatically in Android Studio using Kotlin

I am trying to add a color to the switches in Android Studio using Kotlin I tried few answers fronm this forum and couldn't get it to work

Is it possible to make this work programmatically?

Switch color

I modified my code as mentioned in answer @vishnu

Full code is below:

class MainActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n", "ResourceType", "UseSwitchCompatOrMaterialCode")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        switchButton1.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton1.text = "Switch 1 ON"
                switchButton1.thumbDrawable.setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN)
                Toast.makeText(this, "First Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton1.text = "Switch 1 OFF"
                switchButton1.thumbDrawable.setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN)
                Toast.makeText(this, "First Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        switchButton2.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton2.text = "Switch 2 ON"
                Toast.makeText(this, "Second Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton2.text = "Switch 2 OFF"
                Toast.makeText(this, "Second Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        buttonResetSwitch.setOnClickListener{
            switchButton1.isChecked = false
            switchButton2.isChecked = false
        }
    }
}

I am not seeing the color while loading the app. This color change happens only after activating the switch. How can I get the color change while loading the app (as shown in "Switch OFF" position)

Switch Color Change



Solution 1:[1]

You can use this condition for kotlin. You should try this.

// condiotion while switch is on 
if(mySwitch.isChecked){
    mySwitch.setThumbResource(getColor(R.color.yourcolor))
    mySwitch.setTrackResource(getColor(R.color.yourcolor))
} 
// Condition while switch is off
else {
    mySwitch.setThumbResource(getColor(R.color.yourcolor))
    mySwitch.setTrackResource(getColor(R.color.yourcolor))
}

Solution 2:[2]

The switch color follows the textColor attribute. You can use setSwitchTextAppearance to change the textColor.

From the same Switch docs, the textAppearance and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance and the related setSwitchTypeface() methods control that of the thumb.

Solution 3:[3]

If its a material switch

mySwitch.setTrackTintList("your_color")
mySwitch.setThumbTintList("your_color")

or like this

if (isChecked) {
   mySwitch.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN);
} else {
   mySwitch.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bhavin Solanki
Solution 2 jloh
Solution 3 Vishnu V S