0

In Android, is it possible to have a Switch that can have different images set for the thumb's checked and unchecked states?

For example, when a Switch is in the unchecked state, I'd like the thumb to use image_1.png and when the Switch is in the checked state, I'd like the thumb to use image_2.png.

enter image description here

I used the following guide to set a Switch's thumb to an image, but the image applies to both the checked and unchecked states, I'd like to have a separate image for both states.

Thanks!

0xCursor
  • 2,242
  • 4
  • 15
  • 33

1 Answers1

0

From this post, you can do something like the following:

yourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Set to image_2.png
            yourSwitch.setThumbResource(image2); // <-- Using an image from drawable
        } else {
            // Set to image_1.png
            yourSwitch.setThumbResource(image1);
        }
    }
});

And if you need to convert your Switch to programmatic form:

Switch yourSwitch = (Switch) findViewById(R.id.yourSwitch);
0xCursor
  • 2,242
  • 4
  • 15
  • 33