Listview Item background colour change when long press for Android.

kishukishu INMember ✭✭
edited September 2018 in Xamarin.Forms

I am trying to change the background color of list item when long-press of it, but existing color coding is not allowing.
I added styles in Android everything is working if I remove my default background, but default background color should be there as per UX.
Here is my code,

<DataTemplate x:Key="ResourceDataTemplate">
        <ViewCell BindingContextChanged="Resource_BindingContextChanged">
            <StackLayout Spacing="0" BackgroundColor="#fffff">
                <Grid.RowDefinitions>

                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <StackLayout BackgroundColor="{Binding BgColor}" Style="{StaticResource GeneralPaddingStackLayoutStyle}" Orientation="Vertical" Spacing="0">
                    <Label Text="{Binding LinkName}" Style="{StaticResource titleLabel}" LineBreakMode="TailTruncation">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Tapped="Label_Tapped" CommandParameter="{Binding .}" />
                        </Label.GestureRecognizers>
                    </Label>
                    <customRenderPage:TwoLineLabel Text="{Binding Description}" Style="{StaticResource subTitleLabel}" Margin="{StaticResource defaultTopPaddingforTitle}" />
                </StackLayout>
                <usercontrols:SeparatorGrid Grid.Row="1" />
            </StackLayout>
        </ViewCell>
    </DataTemplate>`

If I remove background color from this line "<StackLayout Spacing="0" BackgroundColor="#fffff">", it's working fine. Otherwise white color overriding the highlighted color, how to overcome this.
Thanks in advance.

Answers

  • kishukishu INMember ✭✭
    edited September 2018

    Here is the ViewCell code,

    <ViewCell BindingContextChanged="Resource_BindingContextChanged">
    
                <StackLayout Spacing="0" BackgroundColor="{Binding BgColor}">
                    <StackLayout Style="{StaticResource GeneralPaddingStackLayoutStyle}" Orientation="Vertical" Spacing="0">
                        <Label Text="{Binding LinkName}" Style="{StaticResource titleLabel}" LineBreakMode="TailTruncation">
                            <Label.GestureRecognizers>
                                <TapGestureRecognizer Tapped="Label_Tapped" CommandParameter="{Binding .}" />
                            </Label.GestureRecognizers>
                        </Label>
                        <customRenderPage:TwoLineLabel Text="{Binding Description}" Style="{StaticResource subTitleLabel}" Margin="{StaticResource defaultTopPaddingforTitle}" />
                    </StackLayout>
                    <usercontrols:SeparatorGrid />
                </StackLayout>
            </ViewCell>`
    

    If I remove BackgroundColor="{Binding BgColor}" this code working fine, I want the solution without removing this color.

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @kishu

    How do you change the color? Could you provide the project?

  • kishukishu INMember ✭✭

    @BillyLiu using below code for Android specific,
    <style name="MyTheme" parent="MyTheme.Base"> <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item> </style> <color name="ListViewHighlighted">#A9A9A9</color>

  • nirav_vikaninirav_vikani Member ✭✭

    I also want the same things in the project. But I don't find any solution yet. Is there anyone know how to do it?

Sign In or Register to comment.