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?
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)