Share via


VisualElement Legacy Color Mode on Android

Some of the Xamarin.Forms views feature a legacy color mode. In this mode, when the IsEnabled property of the view is set to false, the view will override the colors set by the user with the default native colors for the disabled state. For backwards compatibility, this legacy color mode remains the default behavior for supported views.

This Android platform-specific disables this legacy color mode, so that colors set on a view by the user remain even when the view is disabled. It's consumed in XAML by setting the VisualElement.IsLegacyColorModeEnabled attached property to false:

<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        ...
        <Button Text="Button"
                TextColor="Blue"
                BackgroundColor="Bisque"
                android:VisualElement.IsLegacyColorModeEnabled="False" />
        ...
    </StackLayout>
</ContentPage>

Alternatively, it can be consumed from C# using the fluent API:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...

_legacyColorModeDisabledButton.On<Android>().SetIsLegacyColorModeEnabled(false);

The VisualElement.On<Android> method specifies that this platform-specific will only run on Android. The VisualElement.SetIsLegacyColorModeEnabled method, in the Xamarin.Forms.PlatformConfiguration.AndroidSpecific namespace, is used to control whether the legacy color mode is disabled. In addition, the VisualElement.GetIsLegacyColorModeEnabled method can be used to return whether the legacy color mode is disabled.

The result is that the legacy color mode can be disabled, so that colors set on a view by the user remain even when the view is disabled:

Legacy color mode disabled

Note

When setting a VisualStateGroup on a view, the legacy color mode is completely ignored. For more information about visual states, see The Xamarin.Forms Visual State Manager.