androidxamarin.formsxamarin.androidstylingandroid-switch

Switch color is not respected/damped - turn off effect on trackTint color (OnColor)


Normally you would use the OnColor like this

<Switch x:Name="optionSwitch" HorizontalOptions="StartAndExpand" OnColor="Blue" ThumbColor="Cyan" />

to customize the track color and you get the following result:

IsToggled = true; IsToggled

IsToggled = false; Not IsToggled

But in my application the OnColor is always overwritten and I don't know the cause for this (something in Theme.Material.Light.DarkActionBar?).

This is the look and feel in my app with the same code:

IsToggled = true; IsToggled

IsToggled = false; Not IsToggled

So the track color is not the color I set. It seems that there is an "effect", which modifies the color. I thought I could use a custom renderer and change the values for TrackTintMode:

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

    if (Control != null)
    {
        Control.TrackTintMode = Android.Graphics.PorterDuff.Mode.SrcOver;
    }
}

But with this the off color is also set and on re-enabling it goes back to default. I tried many other things but the post would get very long with this ...

How can I turn this "feature" off?

Edit:

After many tries I think I can reproduce the issue. The MainActivity.cs has to look like this:

[Activity(Label = "TestSwitch", Icon = "@mipmap/icon", Theme = "@style/AppTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    // ...
}

And the according theme in styles.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">

    </style>
</resources>

The rest is based on the default XF template.


Solution

  • I was able to reproduce this issue.

    enter image description here enter image description here

    Originally, the default Xamarin.Forms Android project used an older style of control rendering that was common prior to Android 5.0. Applications built using the template have FormsApplicationActivity as the base class of their main activity.

    Xamarin.Forms Android projects now use FormsAppCompatActivity as the base class of their main activity.

    Change:

    global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    

    To:

    global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    

    And use the style.xml like below.

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    After changing, you would get the color you want.

    enter image description here enter image description here