Maturano
Maturano

Reputation: 1023

xamarin forms: Couldn't change the background color of selected item on ListView

I'm trying to remove or at least change the color of the selected item on a ListView (Master Detail).

I didn't see any option to change the background color of the selected item.

Here is what I have done so far:

        <ListView ItemsSource="{Binding MenuItems}"
                  HasUnevenRows="True"
                  RowHeight="50"
                  >
            <ListView.Behaviors>
                <e:EventToCommandBehavior EventName="ItemTapped" 
                                          Command="{Binding NavigateCommand}"
                                          EventArgsParameterPath="Item.ViewName">

                </e:EventToCommandBehavior>
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout VerticalOptions="Center" HorizontalOptions="FillAndExpand"
                            Orientation="Horizontal"
                            Padding="20,0,0,0"
                            Spacing="10"
                            HeightRequest="50">

                            <Image Source="{Binding Icon}"
                            WidthRequest="40"
                            HeightRequest="40"
                            VerticalOptions="Center" />

                            <Label Text="{Binding Title}"
                            FontSize="Medium"
                            VerticalOptions="Center"
                            TextColor="Black"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

When I select any item of the listview, the color keeps orange.

enter image description here

Does anybody know a workaround for this?

Upvotes: 2

Views: 2607

Answers (1)

charlin agramonte
charlin agramonte

Reputation: 707

In Android, you can do that going to your Resources/Value/Style.xml file, and add this code:

     <resources>
       <style name="MyTheme" 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>
<color name="ListViewSelected">#96BCE3</color>
<color name="ListViewHighlighted">#E39696</color>
</resources>

Upvotes: 9

Related Questions