13

I'm creating a ListView that has some simple items inside a ViewCell.

When I select one of the items it becomes orange. When I click and hold (to open the context actions) it becomes white...

background color

<ListView ItemsSource="{Binding Items}" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem Text="Delete" />
                </ViewCell.ContextActions>
                <StackLayout Orientation="Horizontal" Padding="20">
                    <StackLayout HorizontalOptions="StartAndExpand">
                        <Label Text="{Binding Name}" FontSize="Large" FontAttributes="Bold" />
                        <Label Text="{Binding Description}" />
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

How can I customize these colors?

BrunoLM
  • 88,362
  • 76
  • 272
  • 427
  • I spent a lot of hours to find a solution for customizing the selected item color - without success. There are some workarounds on SO and the Xamarin forums but none of them made me happy or they simply didn't work. After a long while I decided to delete the selected item state as soon as the corresponding event is being fired and change the text color of the item instead in order to mark the selected one. – Wosi Sep 09 '15 at 22:41
  • @Wosi hey, check out my answer, might be useful for you :) – BrunoLM Sep 11 '15 at 01:12
  • Possible duplicate of [Xamarin.Forms ListView: Set the highlight color of a tapped item](http://stackoverflow.com/questions/25885238/xamarin-forms-listview-set-the-highlight-color-of-a-tapped-item) – Jannie Theunissen Dec 29 '16 at 09:30

2 Answers2

27

I found out that I have to customize it directly on Android.

To use the theme I changed Droid/Properties/AssemblyInfo.cs adding:

[assembly: Application(Theme = "@style/AppStyle.Light")]

And I created some files on:

Droid\Resources\values

colors.xml contains the color definitions for my theme:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="ListViewSelected">#96BCE3</color>
  <color name="ListViewHighlighted">#E39696</color>
</resources>

styles.xml contains the theme settings:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="AppStyle.Light" parent="android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:colorPressedHighlight">@color/ListViewSelected</item>
    <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
    <item name="android:colorFocusedHighlight">@color/ListViewSelected</item>
    <item name="android:colorActivatedHighlight">@color/ListViewSelected</item>
    <item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item>
  </style>
</resources>

Using these names I can change the listview style.

android:colorPressedHighlight
android:colorLongPressedHighlight
android:colorFocusedHighlight
android:colorActivatedHighlight
android:activatedBackgroundIndicator

References can be found on developer.android.com R.attr

colors

BrunoLM
  • 88,362
  • 76
  • 272
  • 427
  • 1
    Downvoted because it is a Android-only solution, it doesn't answer the question. – 476rick Nov 02 '16 at 13:18
  • Maybe your problem is already solved, but I'll answer my solution for iOS: I've created a `ViewCellRenderer`, overrode `UIKit.UITableViewCell GetCell` function, and set `SelectionStyle` to `None`. If anyone intrested I can send detailed sample code. – anitteb Nov 06 '18 at 10:45
0

Late to the party but the problem has been solved in the CollectionView https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#change-selected-item-color

    <ContentPage.Resources>
        <Style TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="LightSkyBlue" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
Rick Neeft
  • 45
  • 7