Download Code

In this tutorial you will learn how to use custom ListView android application.In Previous tutorial you have learnt to create List View programmatically using ListActivity.So,now you will learn using some xml code.You can say it’s a custom listview.

Custom ListView Android

List View is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter.

Array Adapter

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

Create new custom listview android application project.Drag and Drop List View in Main.axml file.

listview in xamarin,xamarin listview android,custom listview xamarin, how to design custom listview in xamarin,custom listview android xamarin, arrayadapter in xamarin android,arrayadapter,

Create new layout item.axml and place ImageView,TextView.

listview in xamarin,xamarin listview android,custom listview xamarin, how to design custom listview in xamarin,custom listview android xamarin, arrayadapter in xamarin android,arrayadapter,

Go to your MainActivity.cs class.

  • Create variable of ListView.
  • Create String array.
  • Refer id of List View.
  • store string array in ArrayAdapter.
  • Set ArrayAdapter to List View.
  • Apply click event on list view items.
  • Display Toast to show which item is clicked.
using Android.App;
using Android.Widget;
using Android.OS;

namespace XamarinListView
{
	[Activity(Label = "XamarinListView", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{

		ListView codecList;   // list view variable
							  // string array for list menu
		string[] androidCodec = new string[] { "Java", "C#", "C++", "Php", "Javascript", "Android", "Codec" };

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			codecList = (ListView)FindViewById(Resource.Id.list);   // refering list view of xml
																	//setting array adapter with item layout
			ArrayAdapter adapter = new ArrayAdapter(this, Resource.Layout.item, Resource.Id.itemTxt, androidCodec);
			codecList.SetAdapter(adapter);  // setting adapter to list view
			codecList.ItemClick += CodecList_ItemClick;
		}

		void CodecList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
		{
			Toast.MakeText(this, androidCodec[e.Position], ToastLength.Short).Show();
		}
	}
}

Now run your android application project.It will display a list of array which you initialized at the start.When you click on any list item.It will display that item text in Toast.

listview in xamarin,xamarin listview android,custom listview xamarin, how to design custom listview in xamarin,custom listview android xamarin, arrayadapter in xamarin android,arrayadapter,

That’s it for this tutorial.

For Java example Android Codec.

Thanks.