xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

Rendering Issue with Xamarin Forms Switch component on Android only #10415

Open vsfeedback opened 4 years ago

vsfeedback commented 4 years ago

This issue has been moved from a ticket on Developer Community.


After updating to latest versions of Visual Studio 2019, Xamarin.Forms and Xamarin.Android etc, the Xamarin Forms control component 'Switch' is not displaying the toggle button part of the component on Android (all ok on UWP). As you can see in the attached image the Switch background is present and it toggles ok but the actual toggle button part is missing. Any ideas? I do not have a custom render etc.

Xamarin.Forms 4.5.0.530. Visual Studio 2019 16.5.2


Original Comments

Visual Studio Feedback System on 4/15/2020, 03:31 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Ian Weeks on 4/20/2020, 06:52 AM:

Having the same issue. Not able to see the toggle part of the switch control.

VS2019 16.5.4

Xamarin 4.5.0.617


Original Solutions

finch_matt solved on 4/20/2020, 06:59 AM, 1 votes:

Hi...I have a workaround at the moment where I redraw with custom renderer in Android. I'll use this workaround until I can work out alternative or bug fixed...here is code if it helps

[assembly: ExportRenderer(typeof(TextSwitch), typeof(TextSwitchRenderer))] namespace Otopod_BLE.Droid { public class TextSwitchRenderer : SwitchRenderer { private TextSwitch view; private Android.Graphics.Color greyColor = new Android.Graphics.Color(0xF1, 0xF1, 0xF1); private Android.Graphics.Color greenColor = new Android.Graphics.Color(0x46, 0xbd, 0xbf); public TextSwitchRenderer(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 = (TextSwitch)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.SetColorFilter(greenColor, PorterDuff.Mode.Multiply); } protected void SetColorToOffSwitch() { this.Control.ThumbDrawable.SetColorFilter(greyColor, PorterDuff.Mode.Multiply); } }

Ian Weeks solved on 4/20/2020, 07:44 AM, 0 votes:

That workaround does the trick with a couple small tweeks. Thanks!

jsuarezruiz commented 4 years ago

I attached a reproduction sample. Issue10415.zip

I was unable to reproduce the problem. Could we see the original issue screenshot or have an attached reproduction sample?

jackbrockley commented 4 years ago

I had this issue because my main activity inherited from FormsApplicationActivity.

PureWeen commented 4 years ago

@brxtr can you update to appcompat?

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/appcompat-material-design

PureWeen commented 4 years ago

Probably regressed by https://github.com/xamarin/Xamarin.Forms/pull/10155

jackbrockley commented 4 years ago

@PureWeen Indeed, that fixed it for me. I don't know about the original ticket poster though.

dionisoliveira commented 4 years ago

Hey bro..

Can you solve this issue?

I have same problem and i found a workaround:

Create a new classe inheritance with Switch.

public class CustomSwitch: Switch.

After that i create a custom render.

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))] namespace MyNameSpace { public CustomSwitchRenderer(Context context) : base(context) {

}

//Apply my color. protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); if (Control.Checked && Control.Enabled) Control.ThumbDrawable.SetColorFilter(new Android.Graphics.Color(52, 152, 219), PorterDuff.Mode.SrcAtop); else Control.ThumbDrawable.SetColorFilter(new Android.Graphics.Color(245, 245, 245), PorterDuff.Mode.SrcAtop); }

}.

This workaround solution my problem. This problem dindin't happen on all android devices