In a similar vein to my previous post, I recently had to set the colour for a Xamarin Forms Switch’s true/false colour to something other than the default, on Android. The colour scheme for the app meant that on Android, the default false colour caused the Switch’s track to blend into the background, making it look like a circle floating in the middle of nowhere:
iOS, of course, was fine:
Switching Colours Using an Effect
Rather than use a custom renderer for this one, I thought it could better be handled as an effect, and a bit of googling and stack-overflowing bore this out. There’s some very good examples of effects at the Xamarin Forms Community Toolkit.
The switch-colour effect example they have there got me close, but wasn’t quite enough to seal the deal, but I’ll get to that shortly.
The Xamarin Forms switch-colour effect is pretty simple. It uses a basic Effect ‘skeleton’ and adds three bindable properties to it, a TrueColorProperty, a FalseColorProperty and a ThumbColorProperty. The last one enables us to set the colour of the actual ‘thumb-slider’ component of the switch control, whereas the true and false colours are for the ‘track’ within which the slider sits.
Here’s the code for the Xamarin Forms abstract ChangeColorSwitchEffect. Nothing special, just those bindable properties, some getters and setters for them, and attachment/detachment code for when the Switch control is bound at runtime.
For the sake of brevity, I’ve removed some of the duplicate-looking stuff. The fully working example is linked at the end of this post.
XAML Implementation
Now just define a switch as you normally would, and then add values for the BindableProperties you defined above:
Those colours will be a little garish. But as you’ll see, they make it pretty clear what we’ve done in the platform-specific effect implementations. So let’s have a look at those next.
Android Implementation
I’ve left out some of the usings declarations and the effect-export stuff to keep this shorter.
A couple of points:
On Android, if you make the track and thumb colours the same, it doesn’t come out quite like you expect. It looks flat and undifferentiated, because the track has no ‘depth’ beneath the thumb-slider. So I’ve made the track a little darker than the thumb slider by reducing the luminosity (‘brightness’) by 25%.
The example project shows a few extra things you can do with the the thumb-colour, for instance changing the thumb-colour depending on the state of the switch.
Yes, I know I could have used the newer State array stuff to set tint/hint colours on Android, but this is backwardly compatible, AND enables one to change the colour of the thumb-slider based on the switch-state.
iOS Implementation
If you want to change the background colour of the switch, then you need to set a corner-radius on the UISwitch layout, or it draws the component with squared-off corners. See the example code for how to do this.
Unfortunately on iOS it’s not easy to change the thumb-slider colour depending on the switch-state
Similarly to Android, I made the track colours (TintColor is ‘off’ or ‘false’, OnTintColor is ‘on’ or ‘true’) a bit dimmer, by 25%. One wonders why they didn’t call it OffTintColor…
So how does it look?
On Android
On iOS
There’s a simple Xamarin Forms project over in GitHub so you can try it out for yourself. As usual it’s published under the MIT licence, so you can do what you like with the code. Hopefully it can save you some time, or get you where you need to be ;)