0

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.

testing
  • 19,681
  • 50
  • 236
  • 417
  • I checked the code with the Theme of `Theme.Material.Light.DarkActionBar`. But still get the customized track color. Please check my screenshot: https://imgur.com/aBlrCBI Could you provide more details for me to reproduce? – Wendy Zang - MSFT May 06 '21 at 06:00
  • So I am not sure if what I ran into before would be applicable here, but this SO post I made (and later figured out) might help you. https://stackoverflow.com/questions/63836699/switch-not-obeying-style-oncolor-setting the gist of the issue iirc is that the styles.xml (of the android project) were overriding whatever you set in the control. However the resource styles we define for xamarin would override those. Again this seems similar, but I am not sure if it is the exact issue – SomeStudent May 06 '21 at 13:10
  • @SomeStudent: I saw your thread before and tried the Android part, because on iOS it's working fine. `TrackTintList` behaved similar to `TrackTintMode` in my tests. First, the color is correct, but after toggling on and off the default comes back ... I deleted all styles in `styles.xml` and still the behavior occurs. Something is overwriting it as you state, but I haven't found it out. – testing May 06 '21 at 13:18
  • @WendyZang-MSFT: I can make a shrinked down project, but I don't want to share the project officially. Do you have another way to contact you? – testing May 06 '21 at 13:19
  • You could create a simple example for us to reproduce without any personal informations. And according to our policy, we could not provide any contact details. You could upload the project on Github and provide the link for us. – Wendy Zang - MSFT May 07 '21 at 08:01
  • @WendyZang-MSFT: I edited my question, which should include the details to reproduce the issue. How did your code look like? – testing May 07 '21 at 13:28
  • @testing You could try to use the ` – Wendy Zang - MSFT May 11 '21 at 09:00
  • @WendyZang-MSFT: The sample app doesn't show any difference. My real app crashes with *System.NullReferenceException: Object reference not set to an instance of an object. at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.3(intptr,intptr,intptr)*. – testing May 11 '21 at 09:34
  • I double checked the style and the xaml. I was not able to reproduce this issue. – Wendy Zang - MSFT May 13 '21 at 09:09
  • @WendyZang-MSFT: Here is my [sample project](https://github.com/testing-so/TestSwitch). I have the problem in simulator as well as on device. – testing May 13 '21 at 12:41

1 Answers1

1

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

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • First, I thank you very much for helping me out! This works in the sample project! Unfortunately, in my real project I have a customized action bar. With the settings you provided it seems I can't have it. How do I change the theme to get my customized action bar back again? Do you know a transition guide or something like this? – testing May 14 '21 at 11:51
  • I need more details of your ` action bar`. – Wendy Zang - MSFT May 18 '21 at 08:13
  • *AppTheme* has a `actionBarStyle`, where the style's parent `@android:style/Widget.Holo.Light.ActionBar` is. The style has some properties like `background`, `actionModeBackground`, `textColorPrimary` and so on. Don't know if the `Toolbar` is the replacement for my action bar of if I can customize the built-in action bar. – testing May 18 '21 at 08:22
  • I suggest to use the Toolbar to replace the actionbar. In the style i provided, setting the `android:windowActionBar` to false is used to disable the the default ActionBar. – Wendy Zang - MSFT May 18 '21 at 09:17
  • Is this [guide](https://devblogs.microsoft.com/xamarin/android-tips-hello-material-design-v7-appcompat/) still up to date? Inheriting from `ActionBarActivity` seems odd for me. – testing May 18 '21 at 11:55
  • You could use it as well. But for new, as of Xamarin.Forms 4.6 the theme has moved into the Forms binary. If you want to override anything you can do that in style with the latest version of your VS and Xamarin.forms. – Wendy Zang - MSFT May 20 '21 at 07:37
  • Do you have a link for a how to? – testing May 20 '21 at 08:15
  • Upload on Github and give the link. https://github.com/ – Wendy Zang - MSFT May 20 '21 at 08:32
  • Perhaps I did get it wrong ... I can style the toolbar and so on from Android directly in my Xamarin.Forms project? Because currently some things like the toolbar style are directly in the Android project and the other things are in the Xamarin.Forms project. – testing May 20 '21 at 08:40
  • For now, we use `androidx.appcompat.widget.Toolbar`. You could use this to create a toolbar layout and then set in `ToolbarResource = Resource.Layout.Toolbar;` – Wendy Zang - MSFT May 20 '21 at 08:47
  • OK I haven't migrated to AndroidX yet ... In [another thread](https://stackoverflow.com/questions/67075448/xamarin-androidx-migration-crash-on-app-startup-system-invalidcastexception-a) I saw that toolbar.axml had to be deleted. Thanks, I have to dig deeper in this area. – testing May 20 '21 at 09:01