Create Custom ListView In Xamarin With Visual Studio 2015


In Android Application Development, sometimes we need to use ListView, which has content images, as well as List item or ListView with the button. For this, we need to create custom ListView. In this article, I am creating custom ListView, which has both the content images and items.

The steps are given below to create custom ListView.

Step 1

Create new project for an Android Application

I have selected “Blank App(Android)” template for this article.

Step 2

Application layout

I have used two Android layouts to create custom ListView. First is “main.axml” layout, which is contact ListView, and second is “list_single.axml”, whose contents are ListView and ImageView, using “Table Layout” View. AXML code of both layouts is given below.


  1. <?xml version="1.0" encoding="utf-8"?>  given
  2. <LinearLayout xmlns:android=""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:minWidth="25px"  
  7.     android:minHeight="25px">  
  8. <ListView  
  9.     android:minWidth="25px"  
  10.     android:minHeight="25px"  
  11.     android:layout_width="match_parent"  
  12.     android:layout_height="match_parent"  
  13.     android:id="@+id/listView" />  
  14. </LinearLayout>  


  1. <?xml version ="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android=""  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5. <TableRow>  
  6. <ImageView  
  7.     android:id="@+id/img"  
  8.     android:layout_width="50dp"  
  9.     android:layout_height="50dp" />  
  10. <TextView  
  11.     android:id="@+id/txt"  
  12.     android:layout_width="wrap_content"  
  13.     android:layout_height="50dp" />  
  14. </TableRow>  
  15. </TableLayout>  

I have taken 10 items in ListView with its corresponding image, so I have added 10 image files in Resource->drawable folder. It is shown below.


Step 3

Create CustomList BaseAdapter class

To create custom ListView, I have created “CustomList” class, which is extended BaseAdapter base class. BaseAdapter requires GetItem(), GetItemId(), GetView and count methods. GetView() method is used to create view of custom ListView and LayoutInflater is used to manipulate Android screen, using predefined XML layout (Here is list_single.axml). This class is used to instantiate layout XML file into its corresponding View objects. It is never used directly. “CustomList” class has three arguments, which are Activity, List Item and corresponding Imageid. GetItemId() and Count method requires count, which is the length of the string object “listitem”. “CustomList” class code snippet is given below.


  1. class CustomList: BaseAdapter  
  2. {  
  3.     private Activity context;  
  4.     private string[] listitem;  
  5.     private int[] imageId;  
  6.     public override int Count {  
  7.         get {  
  8.             return listitem.Length;  
  9.         }  
  10.     }  
  11.     public CustomList(Activity context, string[] listitem, int[] imageId) {  
  12.         this.context = context;  
  13.         this.listitem = listitem;  
  14.         this.imageId = imageId;  
  15.     }  
  16.     public override Java.Lang.Object GetItem(int position) {  
  17.         return null;  
  18.     }  
  19.     public override long GetItemId(int position) {  
  20.         return listitem.Length;  
  21.     }  
  22.     public override View GetView(int position, ViewconvertView, ViewGroup parent) {  
  23.         var view = context.LayoutInflater.Inflate(Resource.Layout.list_single, parent, false);  
  24.         TextView txtTitle = (TextView) view.FindViewById(Resource.Id.txt);  
  25.         ImageView imageView = (ImageView) view.FindViewById(Resource.Id.img);  
  26.         txtTitle.Text = (listitem[position]).ToString();  
  27.         imageView.SetImageResource(imageId[position]);  
  28.         return view;  
  29.     }  
  30. }  


Step 4

Show Custom ListView and ItemClick event

In MainActivity, I have set two arrays, where one string array is for list item (“listitem”) and an image Id (“imageId”). Create an object of “CustomList” class as “adapter” and it is set as Adapter of ListView of main.axml. The code snippet of MainActivity is shown below. 


  1. public class MainActivity : Activity  
  2. {  
  3.    ListView list;  
  4.    string[] listitem = {" C# Corner"," Xamarin"," Google Plus"," Twitter"," Windows"," Bing"," Itunes"," Wordpress","   Drupal"," Whatapp"};  
  5.    int[] imageId = {  
  6.                                Resource.Drawable.image9,  
  7.                                Resource.Drawable.image10,  
  8.                                Resource.Drawable.image1,  
  9.                                Resource.Drawable.image2,  
  10.                                Resource.Drawable.image3,  
  11.                                Resource.Drawable.image4,  
  12.                                Resource.Drawable.image5,  
  13.                                Resource.Drawable.image6,  
  14.                                Resource.Drawable.image7,  
  15.                                Resource.Drawable.image8  
  16.                              };  
  17. protected override void OnCreate(Bundle bundle)  
  18. {  
  19.     base.OnCreate(bundle);  
  20.     SetContentView (Resource.Layout.Main);  
  21.     CustomList adapter = new CustomList(this,listitem, imageId);  
  22.     list = (ListView)FindViewById(Resource.Id.listView);  
  23.     list.Adapter = adapter;  
  24.     list.ItemClick += List_ItemClick;  
  25. }  
  27. private void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)  
  28. {  
  29.      Toast.MakeText(this"You Clicked at " + listitem[e.Position], ToastLength.Long).Show();  
  30. }  
  31. }  

When the user clicks on List item, it shows a message on the screen “You Clicked at” with listitem. For example, if user clicks on item “C# Corner”, display message comes when you click at C# Corner”. We can see this in an output section of this article.




In this article, we learned how to create, show custom ListView and how to write Item click event of ListView in Xamarin with Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all