Forum Cross Platform with Xamarin

Change ListView selected item text color in Xamarin Android

I have a problem with changing ListView SelectedItem's text color using Android styles.xml. I know that I can achieve it by using a custom renderer or create a color property in the model and bind to it using converter, but this is not the way I want. Here is my ListView:

            <ListView
                ItemsSource="{Binding CurrentContract.Contacts}"
                SelectedItem="{Binding CurrentContact}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Label
                                    Grid.Column="0"
                                    Grid.ColumnSpan="2"
                                    FontSize="18"
                                    HorizontalOptions="Start"
                                    Text="{Binding Type}"
                                    VerticalOptions="Center" />
                                <Label
                                    Grid.Column="1"
                                    Grid.ColumnSpan="2"
                                    FontAttributes="Bold"
                                    FontSize="18"
                                    HorizontalOptions="End"
                                    Text="{Binding Value}"
                                    VerticalOptions="Center" />
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Here is my styles.xml:

<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="colorPrimary">@color/AccentColor</item>
<item name="colorPrimaryDark">@color/AccentColor</item>
<item name="colorAccent">@color/AccentColor</item>
<item name="colorControlHighlight">@color/AccentColor</item>
<item name="colorControlActivated">@color/AccentColor</item>
<item name="colorControlNormal">@color/AccentColor</item>

<item name="android:textColor">@drawable/PrimaryTextColorSelector</item>

<item name="android:windowBackground">@color/BackgroundColor</item>
<item name="android:navigationBarColor">@color/AccentColor</item>
<item name="android:colorButtonNormal">@color/ButtonNormalColor</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="android:colorPressedHighlight">@color/AccentColor</item>
<item name="android:colorLongPressedHighlight">@color/AccentColor</item>
<item name="android:colorFocusedHighlight">@color/AccentColor</item>
<item name="android:colorActivatedHighlight">@color/AccentColor</item>
<item name="android:activatedBackgroundIndicator">@color/AccentColor</item>

<item name="android:listDivider">@color/PrimaryTextColor</item>
<item name="android:dividerHeight">4dp</item>

Here is my colors.xml:

<color name="BackgroundColor">#E6E6E6</color>
  <color name="AccentColor">#493FFF</color>
  <color name="ButtonNormalColor">#D5D5D5</color>
  <color name="DisabledTextColor">#66000000</color>
  <color name="PrimaryTextColor">#FF000000</color>
  <color name="PrimaryTextColorInverse">#FFFFFFFF</color>

Here is my PrimaryTextColorSelector.xml:

<item android:state_enabled="false" android:color="@color/DisabledTextColor"/>
  <item android:state_activated="true" android:color="@color/PrimaryTextColorInverse"/>
  <item android:state_selected="true" android:color="@color/PrimaryTextColorInverse"/>
  <item android:color="@color/PrimaryTextColor"/>

When ui element is disabled, text color changes properly. SelectedItem's background changes properly too. When I added this line to my selector:

<item android:state_pressed="true" android:color="@color/PrimaryTextColorInverse"/>

It works properly too (text color of touched ListView item changes while my finger on it, but when I put it off it changes back to common color).

Answers

  • SirAndriySirAndriy USMember ✭✭✭

    This works for me:

    <!-- List view selection color -->
    <item name="android:colorPressedHighlight">@color/yellow</item>
    <item name="android:colorLongPressedHighlight">@color/yellow</item>
    <item name="android:colorFocusedHighlight">@color/yellow</item>
    <item name="android:colorActivatedHighlight">@color/yellow</item>
    <item name="android:activatedBackgroundIndicator">@color/yellow</item>
    
  • NikolayMNikolayM USMember ✭✭
    edited May 2018

    Why not change the color from the XAML codebehind on ListView.ItemSelected event. EventArgs e.SelectedItem cast it and change background color.

  • ExLuzZziVoExLuzZziVo Member ✭✭

    @SirAndriy said:
    This works for me:

    <!-- List view selection color -->
    <item name="android:colorPressedHighlight">@color/yellow</item>
    <item name="android:colorLongPressedHighlight">@color/yellow</item>
    <item name="android:colorFocusedHighlight">@color/yellow</item>
    <item name="android:colorActivatedHighlight">@color/yellow</item>
    <item name="android:activatedBackgroundIndicator">@color/yellow</item>
    

    It changes only selected item background. I need to change text color.

    @NikolayM said:
    Why not change the color from the XAML codebehind on ListView.ItemSelected event. EventArgs e.SelectedItem cast it and change background color.

    Because I want to change it by using styles.xml in android project.

Sign In or Register to comment.