Odd Xamarin Forms Listview contextaction Android behavior

Kelly Kranabetter 1 Reputation point
2021-03-12T20:22:16.973+00:00

I have created a simple ListView with a context menu:

    <ListView x:Name="listview" SelectionMode="Single">  
        <ListView.ItemTemplate>  
            <DataTemplate>  
                <ViewCell>  
                    <StackLayout Orientation="Horizontal" >  
                        <Label Text="{Binding item1}"></Label>  
                        <Label Text="{Binding item2}"></Label>  
                        <Label Text="{Binding item3}"></Label>  
                    </StackLayout>  
                    <ViewCell.ContextActions>  
                        <MenuItem Text="Add" ></MenuItem>  
                        <MenuItem Text="Delete"></MenuItem>  
                        <MenuItem Text="Edit"></MenuItem>  
                    </ViewCell.ContextActions>  
                </ViewCell>  
            </DataTemplate>  
        </ListView.ItemTemplate>  
    </ListView>  

Development environment is Visual Studio 16.9.1, Xamarin Forms Nuget package is at 5.0.0.2012 which I believe are the latest. Pictures below are of the Android Pixel2 Emulator.

Selecting an item works ok:
77348-image.png

A long press on the selected rows activates the menu but disables the selection background. Which row is the operation on?:
77354-image.png

If I tap around randomly on other rows they may select or may deselect:
77289-image.png

When I click the back button to exit the context menu the selections change again (notice the selections are different than the above image):
77349-image.png

If I tap on each item they will deselect and return to a single selection.

I have SelectionMode="Single" and still seem to get multiple selections. What am I doing wrong?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
    2021-03-14T03:41:00.317+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    In android, you can simply add following tag(s) to styles.xml (location: Android project > Resources > values > styles.xml)

       <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>  
    

    And add color in file colors.xml:

    <color name="ListViewHighlighted">#FFFF00</color>  
    

    The result is:

    77497-image.png

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.