Long Press Background Color Change in ListView

Is it possible to change the background color on long press (Context Menu ) in Xamarin Forms in android ?
I have have tried customizing the theme but it doesn't work or maybe I am wrong somewhere !

Answers

  • NishantJhaNishantJha USMember ✭✭

    yes it is possible. But the process of achieving it is quite complex I think. You need to add gesture recognizer to your listview items. Usually what I do is have a boxview or some sort of frame that covers up the background of each listview item and then handle color of that boxview whenever a listview item is tapped. Hope this points you to the right direction

  • ASHISHKUMAR.8068ASHISHKUMAR.8068 USMember ✭✭

    @NishantJha The issue is in long press scenario which fires up menu context menu items.

  • NishantJhaNishantJha USMember ✭✭

    I was giving you an example. I have not implemented long tap, I have only done tap. However, gesture recognizer also includes long press as option.

  • ASHISHKUMAR.8068ASHISHKUMAR.8068 USMember ✭✭

    @NishantJha Yes it includes but it wont fire up menu context ,right ?

  • NishantJhaNishantJha USMember ✭✭

    I'm not sure but my guess is ListView context menu is tied up with the listview item itself. But if you add gesture recognizer to something inside the listview (say boxview) then it should trigger both. if not it will definitely trigger your boxview implementation and you can invoke the context menu for that particular item.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    For our application, changing the theme attributes took care of this.

    These are what we have set. Take what you need.

    <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>
    
  • NishantJhaNishantJha USMember ✭✭

    I am saying that's how I would approach it. If I had an implementation I would have given you the code but I don't.

  • ASHISHKUMAR.8068ASHISHKUMAR.8068 USMember ✭✭

    @AdamMeaney I would give it a try :)

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    As a note, in case you meant the actual context menu bar at the top and not the listview item that was clicked, there are themes for that too.

    In your AppTheme, add these lines:

    <item name="actionModeStyle">@style/ContextActionMode</item>
    
    <style name="ContextActionMode" parent="@style/Widget.AppCompat.ActionMode">
        <item name="background">@color/primary</item>
    </style>
    
  • rio_riyario_riya INMember ✭✭

    @ASHISHKUMAR.8068 have you found the solution?

  • Works for me, but I had to change a little, the name "accentColor" to "colorAccent" (this came from colors.xml)

    <item name="android:colorPressedHighlight">@color/colorAccent</item>
    <item name="android:colorLongPressedHighlight">@color/colorAccent</item>
    <item name="android:colorFocusedHighlight">@color/colorAccent</item>
    <item name="android:colorActivatedHighlight">@color/colorAccent</item>
    <item name="android:activatedBackgroundIndicator">@color/colorAccent</item>
    
Sign In or Register to comment.