BrunoLM BrunoLM - 1 year ago 243
Android Question

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

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">
<MenuItem Text="Delete" />
<StackLayout Orientation="Horizontal" Padding="20">
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Name}" FontSize="Large" FontAttributes="Bold" />
<Label Text="{Binding Description}" />

How can I customize these colors?

Answer Source

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:


colors.xml contains the color definitions for my theme:

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

styles.xml contains the theme settings:

<?xml version="1.0" encoding="utf-8" ?>
  <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>

Using these names I can change the listview style.


References can be found on R.attr


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download