Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
    <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?

like image 880
BrunoLM Avatar asked Sep 09 '15 22:09

BrunoLM


People also ask

How do I select an item in list view?

To select a single list view item, you can use various actions provided by Android ListView object: TouchItem , LongTouchItem , and similar actions – simulate touch or long touch on a specific list view item. SelectItem – selects or unselects the specified list view item.

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


1 Answers

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

like image 151
BrunoLM Avatar answered Sep 24 '22 06:09

BrunoLM