How do I change the color of a selected item on a ListView?

14,610

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

Share:
14,610
BrunoLM
Author by

BrunoLM

I'm a Developer for Fun! Things I like Code Play games Anime / Manga Contact information [email protected] LinkedIn Facebook Site - https://brunolm.github.io/ Blog - http://blog.codingwise.com/

Updated on June 09, 2022

Comments

  • BrunoLM
    BrunoLM about 2 years

    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?

  • 476rick
    476rick over 7 years
    Downvoted because it is a Android-only solution, it doesn't answer the question.
  • anitteb
    anitteb over 5 years
    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.
  • Jaser
    Jaser over 2 years
    I'm intressted, can you send me a sample code please ?