Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Custom Switch renderer : thumb has disappear !

galactosegalactose Member ✭✭

Hi,

I use this custom Switch renderer (for Android) since some months (see code below). It worked very very well some months ago, and I did not modify its class.

I don't know what the problem really is, but since I updated the version of Xamarin.Forms (from... ? don't remember... to 4.5) I noticed, some weeks after, that it does not render the thumb button anymore ! I thought maybe the Android renderer has to be coded diffrently, but I had no error/warning noticed by IntelliSense in the code. So I googled it but I did not find anything about it. So where can be the problem ?

In main solution:

public class CustomSwitch : Switch
{
    public static readonly BindableProperty SwitchOffColorProperty
        = BindableProperty.Create(nameof(SwitchOffColor), typeof(Color), typeof(CustomSwitch), Color.Default);

    public Color SwitchOffColor
    {
        get { return (Color)GetValue(SwitchOffColorProperty); }
        set { SetValue(SwitchOffColorProperty, value); }
    }

    public static readonly BindableProperty SwitchOnColorProperty
        = BindableProperty.Create(nameof(SwitchOnColor), typeof(Color), typeof(CustomSwitch), Color.Default);

    public Color SwitchOnColor
    {
        get { return (Color)GetValue(SwitchOnColorProperty); }
        set { SetValue(SwitchOnColorProperty, value); }
    }

    public static readonly BindableProperty SwitchOnThumbColorProperty
        = BindableProperty.Create(nameof(SwitchOnThumbColor), typeof(Color), typeof(CustomSwitch), Color.Default);

    public Color SwitchOnThumbColor
    {
        get { return (Color)GetValue(SwitchOnThumbColorProperty); }
        set { SetValue(SwitchOnThumbColorProperty, value); }
    }

    public static readonly BindableProperty SwitchOffThumbColorProperty
        = BindableProperty.Create(nameof(SwitchOffThumbColor), typeof(Color), typeof(CustomSwitch), Color.Default);

    public Color SwitchOffThumbColor
    {
        get { return (Color)GetValue(SwitchOffThumbColorProperty); }
        set { SetValue(SwitchOffThumbColorProperty, value); }
    }
}

And in Android solution:

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CardioCALC.Droid.CustomSwitchRenderer))]
namespace CardioCALC.Droid
{
    public class CustomSwitchRenderer : SwitchRenderer
    {
        private CustomSwitch view;

        public CustomSwitchRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null || e.NewElement == null)
                return;
            view = (CustomSwitch)Element;
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
            {
                this.Control.SwitchMinWidth = System.Convert.ToInt32(view.MinimumWidthRequest);

                if (this.Control != null)
                {
                    if (this.Control.Checked)
                    {
                        this.SetColorToOnSwitch();
                    }
                    else
                    {
                        this.SetColorToOffSwitch();
                    }
                }
            }

            this.Control.CheckedChange += (sender, e2) => {
                ((IElementController)base.Element).SetValueFromRenderer(Xamarin.Forms.Switch.IsToggledProperty, Control.Checked);
                if (this.Control.Checked)
                    this.SetColorToOnSwitch();
                else
                    this.SetColorToOffSwitch();
            };
        }


        protected void SetColorToOnSwitch()
        {
            this.Control.ThumbDrawable.SetTint(view.SwitchOnThumbColor.ToAndroid());
            this.Control.TrackDrawable.SetTint(view.SwitchOnColor.ToAndroid());
        }

        protected void SetColorToOffSwitch()
        {
            this.Control.ThumbDrawable.SetTint(view.SwitchOffThumbColor.ToAndroid());
            this.Control.TrackDrawable.SetTint(view.SwitchOffColor.ToAndroid());
        }
    }
}

Thanks for your help,
Galactose

Best Answer

Answers

  • galactosegalactose Member ✭✭
    Hi @Jarvan

    Thanks a lot for your answer. I had no time to try it but I'll let you know very soon.

    Thanks,
    Galactose
  • JarvanJarvan Member, Xamarin Team Xamurai

    Waiting for your update.

  • galactosegalactose Member ✭✭

    Hi @Jarvan , I tried it and it works well !

    Thanks for your help ;)
    Galactose

Sign In or Register to comment.