Background color of selecteditem in listview xamarin.forms
This can be accomplished by using Custom Renderers
iOS Custom Renderer
Edit the SelectionStyle
property on iOS.
Below is an example that sets the UITableViewCellSelectionStyle
to None
.
using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ListViewSample.iOS;
[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.iOS
{
public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
Android Custom Renderer
Create a new drawable,
ViewCellBackground.xml
and save it to theResources
>drawable
folder:<?xml version="1.0" encoding="UTF-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <!--Change the selected color by modifying this hex value--> <solid android:color="#FFFFFF" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#FFFFFF" /> </shape> </item> </selector>
Create Custom Renderer for the ViewCell
using System; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using ListViewSample.Droid; [assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))] namespace ListViewSample.Droid { public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer { protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) { var cell = base.GetCellCore(item, convertView, parent, context); cell.SetBackgroundResource(Resource.Drawable.ViewCellBackground); return cell; } } }
Edit: Removed implementation without Custom Renderers
Sample Xamarin.Forms ListView App
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace ListViewSample
{
public class CustomViewCell : ViewCell
{
public CustomViewCell()
{
View = new Label
{
Text = "Hello World"
};
}
}
public class ListViewContentPage : ContentPage
{
public ListViewContentPage()
{
var itemSourceList = new List<CustomViewCell>();
itemSourceList.Add(new CustomViewCell());
itemSourceList.Add(new CustomViewCell());
var listView = new ListView();
listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
listView.ItemsSource = itemSourceList;
listView.SeparatorVisibility = SeparatorVisibility.None;
Content = listView;
}
}
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new NavigationPage(new ListViewContentPage());
}
}
}
John
I am a cross functional engineer with a strong background in Agile software engineering. I am a strong-willed and determined individual, having been exposed to different social and working environments, I have developed a lot of skills both personally and professionally. I believe that challenges can only strengthen my abilities and not bring me down which is why I always seek out new interesting challenges. I have experience with backend web development, Developing mobile apps with Xamarin and a hint of DevOps knowledge with Kubernetes. I love solving problems and learning recent technologies especially in web and mobile.
Updated on June 22, 2022Comments
-
John about 2 years
When working with ListView in Xamarin.forms, on selecting an item, the item remains like that with a rather ugly background colour. Can i disable this feature?
Here's my code:
<StackLayout> <ListView x:Name="FoodList" HasUnevenRows="True" CachingStrategy="RecycleElement" ItemTapped="OnItemTapped"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Padding="0,15,0,0" > <StackLayout Orientation="Horizontal" BackgroundColor="White" > <Image Source="{Binding image_url}" Aspect="AspectFill" HeightRequest="200" WidthRequest="200"/> <StackLayout Orientation="Vertical"> <Label Text="{Binding food_name}" TextColor="Black" Style="{StaticResource MainLisTtext}" /> <Label Text="{Binding price}" TextColor="Black" Style="{StaticResource SubLisTtext}" /> <Label Text="{Binding food_description}" TextColor="Black" Style="{StaticResource SubLisTtext}" /> </StackLayout> </StackLayout> </StackLayout> </ViewCell>vc </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>
-
John over 7 yearsOnListViewTextCellTapped is this a custom method??
-
Brandon Minnick over 7 yearsYes- Sorry about that! I just updated my answer to include the OnListViewTextCellTapped method.
-
John over 7 yearsOk.When using listview I have access to selected item, does that apply here ?
-
Brandon Minnick over 7 yearsSorry, I don't understand your question. Can you be more specific?
-
John over 7 yearsIf I'm handling listview item selected normally, I have access to Listview.selecteditem.....when using your approach how do I get the selected item??
-
Brandon Minnick over 7 yearsAre you using a Custom ViewCell? e.g.
public class MyCustomViewCell : ViewCell
-
John over 7 yearscheck my code, I used xaml to define my listview template
-
Brandon Minnick over 7 yearsThanks John- it looks like this will be easier to implement by using Custom Renderers. I added the code to implement this using a Custom Renderer on Android.
-
Brandon Minnick over 7 yearsHey John! I just added a sample Xamarin.Forms App that shows this implementation working.
-
John over 7 yearsI'll try it out and get back to you.
-
Brandon Minnick over 7 yearsHey John! Did everything turn out well?
-
John over 7 yearsYeah. It did. Sorry about that
-
Baqer Naqvi almost 7 years@BrandonMinnick I tried it for android. When I click on the item it shows my required background color but it fades away when i release the touch. any idea?
-
Brandon Minnick over 6 yearsHey @BaqerNaqvi! Sorry - I just saw your comment. Are you still trying to solve this? Open a new question and shoot me the link!