question

Ivan-1212 avatar image
0 Votes"
Ivan-1212 asked ColeXia-MSFT edited

SwitchCell - Is it possible to change color of the dot on the switch?

As per the title - is it possible to switch the color of the large dot on the switch through XAML properties of SwitchCell? You can already change the background of the switch with OnColor, but I can't find the color property of the dot - which the default for on android seems to be pink, which doesn't fit with the rest of the colours.

I am aware that there are numerous other ways to do what I want to achieve, but it just seems bizarre that there's no XAML property for it on the SwitchCell, so I wanted to make sure I'm not missing something obvious. (Posting an example picture - the OnColor is DarkGreen).
84996-switchcellexample.png



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT edited

Hello,

Welcome to Microsoft Q&A!

Xamarin.Forms does not provide the API to change thumb color on Switch .

We could create custom renderer for Switch and change the thumb color with property ThumbTintList .

However , we need to use ViewCell instead of SwitchCell first .


Forms

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell >
                    <Switch/>
                </ViewCell>
            </DataTemplate>
</ListView.ItemTemplate>


Android

[assembly: ExportRenderer(typeof(Xamarin.Forms.Switch), typeof(MyRenderer))]
namespace FormsA.Droid
{
    class MyRenderer : SwitchRenderer
    {
        public MyRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.Switch> e)
        {
            base.OnElementChanged(e);

            if(Control != null)
            {
                Control.ThumbTintList = ColorStateList.ValueOf(Android.Graphics.Color.Green);
            }
        }
    }
}


Test

85111-capture.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



capture.png (809 B)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.