Forum Xamarin.Android
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.

How to create standard design of ios switch on android?

kingston5555kingston5555 Member ✭✭✭

I want to create a custom switch on android which looks like a standard ios switch.

Please help me do it

Best Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 26 Accepted Answer

    You could cover the default button with third-party plugin in custom renderer ,and add custom property and event .


    Refer to https://stackoverflow.com/a/63593892/8187800 .

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                SwitchButton switchButton = new SwitchButton(c);
    
    
                switchButton.ThumbColor = ColorStateList.ValueOf(Android.Graphics.Color.Green);
    
                switchButton.CheckedChange += SwitchButton_CheckedChange;
    
                SetNativeControl(switchButton);
    
            }
    
        }
    
        private void SwitchButton_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            var customSwitch = Element as CustomSwitch;
            customSwitch.IsToggle = e.IsChecked;
    
            customSwitch.OnToggled();
    
            (Control as SwitchButton).BackColor = ColorStateList.ValueOf(e.IsChecked? Android.Graphics.Color.Orange: Android.Graphics.Color.LightGray);
    
        }
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 26 Accepted Answer

    You could cover the default button with third-party plugin in custom renderer ,and add custom property and event .


    Refer to https://stackoverflow.com/a/63593892/8187800 .

  • kingston5555kingston5555 Member ✭✭✭
    How can I change color of switch to make it like on the picture?
  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                SwitchButton switchButton = new SwitchButton(c);
    
    
                switchButton.ThumbColor = ColorStateList.ValueOf(Android.Graphics.Color.Green);
    
                switchButton.CheckedChange += SwitchButton_CheckedChange;
    
                SetNativeControl(switchButton);
    
            }
    
        }
    
        private void SwitchButton_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            var customSwitch = Element as CustomSwitch;
            customSwitch.IsToggle = e.IsChecked;
    
            customSwitch.OnToggled();
    
            (Control as SwitchButton).BackColor = ColorStateList.ValueOf(e.IsChecked? Android.Graphics.Color.Orange: Android.Graphics.Color.LightGray);
    
        }
    

Sign In or Register to comment.