0

I want to change the inside color of a switch button to green when it is checked but its only change the background color . Here is my code. Please help me . I want to change the color only through programatically

switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {

switchButton.setBackgroundColor(Color.GREEN)

}
}
Dev
  • 25
  • 11

1 Answers1

0

You probably want to set the track drawable using https://developer.android.com/reference/android/widget/Switch.html#setTrackDrawable(android.graphics.drawable.Drawable). You should be able to just use a ColorDrawable instance:

switchButton.setTrackDrawable(new ColorDrawable(Color.BLACK));

EDIT: try using https://developer.android.com/reference/android/widget/Switch.html#setThumbDrawable(android.graphics.drawable.Drawable) or https://developer.android.com/reference/android/widget/Switch.html#setThumbResource(int)? One thing to note is that if you replace the thumb drawable with a ColorDrawable it may not actually look the same: not sure you can only change the color with a simple drawable, you may need to create a more complex XML Drawable to do that.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • It's only change the button background – Dev Apr 08 '17 at 14:59
  • Did you mean switchButton.setThumbTintList(Color.GREEN); It shows an error – Dev Apr 08 '17 at 15:12
  • No, thumb resource or drawable: tint list is different. – Femi Apr 08 '17 at 15:13
  • I don't want to add custom switch. I just want to edit the color of default switch button. Default switch button shows red color when it is checked, I want to make it as green – Dev Apr 08 '17 at 15:19
  • Ah. See http://stackoverflow.com/questions/11253512/change-on-color-of-a-switch: maybe that helps. – Femi Apr 08 '17 at 15:41