JKennedy
JKennedy

Reputation: 18799

How do I change the selected item colour using Styles?

I have a Android 5 device and I set up some Material Styles using this blog:Material Design Theming for Xamarin.Forms Android Apps and got this theme:

Values-v21\styles.xml

<resources>
  <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/color_primary</item>
    <item name="android:colorPrimaryDark">@color/color_primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:colorControlNormal">@color/color_primary</item>
    <item name="android:colorControlActivated">@color/color_primary</item>
    <item name="android:colorControlHighlight">@color/color_primary</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>
</resources>

Values\colors.xml

<resources>
  <color name="color_primary">#404040</color>
  <color name="color_primary_dark">#404040</color>
  <color name="accent">#379ADE</color>
</resources>

This works fine.. BUT.. I cant seem to work out why my selected item is appearing orange?

enter image description here

How do I change the color of my selected item using Styles.. Where is the Orange coming from?

Ps. if I remove the Styles it returns to the default blue.

I also found Android Color Control Cheat Sheet but can't see an entry to change listview selected item

Upvotes: 2

Views: 1888

Answers (1)

Sten Petrov
Sten Petrov

Reputation: 11040

It looks like you're having Xamarin.Forms Styles and Android resources mixed up.

In Xamarin.Forms you can change the color based on a property change like this:

   <ResourceDictionary>
     <Style TargetType="Entry"> 
       <Style.Triggers>
         <Trigger TargetType="Entry" Property="IsFocused" Value="True">
             <Setter Property="BackgroundColor" Value="Yellow" />
         </Trigger>
       </Style.Triggers>
     </Style>
   </ResourceDictionary>

You may want to define your colors Xamarin.Forms <ResourceDictionary> (not Android <resource>) and maybe populate the dictionary on startup time from platform-specific resources.

A general side note: when using Xamarin.Forms really focus on your app's functionality in a platform-independent manner; from experience I can say it's difficult and messy to retroactively make the app platform-independent when you've taken subtle dependencies on the platform, such as colors, sizes, UI paradigms etc.

Upvotes: 2

Related Questions