Creating A List View In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below are required to be followed in order to create a ListView in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Visual Studio

Step 2

After opening New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app; a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Visual Studio

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and source in your project. Now, select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source. Choose the Designer Window, if you want the designer to design your app.

Visual Studio

Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire.

Visual Studio

Now, delete the default "hello world" button. Go to the source panel and you need to delete the button coding. After deleting XAML code, delete C# button action code. Go to MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window and scroll down. You will see all the tools and controls. You need to drag and drop the ListView.

Visual Studio

Step 6

Now, go to the properties Window. You need to edit the ListView's Id value (EX android:id="@+id/listview" ).

Visual Studio

Step 7

In this step, go to the Main.axml page source panel. Note, ListView's Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <ListView android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview" /> </LinearLayout>  
Visual Studio

Step 8

In this step, go to MainActivity.cs page in Solution Explorer. Add one Namespace and add two variables.

MainActivity.cs
  1. using System.Collections.Generic;  
  2. //Variables  
  3. private ListView listnames;  
  4. private List < string > itemlist;  
Visual Studio

Step 9

In this step, go to the MainActivity.cs page in Solution Explorer. Write the code, mentioned below Between OnCreate() Method.

MainActivity.cs
  1. using System.Collections.Generic;  
  2. namespace list_view {  
  3.     [Activity(Label = "list_view", MainLauncher = true, Icon = "@drawable/icon")]  
  4.     public class MainActivity: Activity {  
  5.         private ListView listnames;  
  6.         private List < string > itemlist;  
  7.         protected override void OnCreate(Bundle bundle) {  
  8.             base.OnCreate(bundle);  
  9.             // Set our view from the "main" layout resource  
  10.             SetContentView(Resource.Layout.Main);  
  11.             listnames = FindViewById < ListView > (Resource.Id.listview);  
  12.             itemlist = new List < string > ();  
  13.             itemlist.Add("C");  
  14.             itemlist.Add("C++");  
  15.             itemlist.Add("JAVA");  
  16.             itemlist.Add("C#");  
  17.             itemlist.Add("F#");  
  18.             itemlist.Add("PYTHON");  
  19.             itemlist.Add("SWIFT");  
  20.             ArrayAdapter < string > adapter = new ArrayAdapter < string > (this, Android.Resource.Layout.SimpleListItem1, itemlist);  
  21.             listnames.Adapter = adapter;  
  22.             listnames.ItemClick += Listnames_ItemClick;  
  23.         }  
  24.         private void Listnames_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {  
  25.             Toast.MakeText(this, e.Position.ToString(), ToastLength.Long).Show();  
  26.         }  
  27.     }  
  28. }  
Visual Studio

Step 10

If you have an Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Visual Studio

Output

After few seconds, the app will start running on your phone.

You will see that the ListView works successfully.

Visual Studio

Visual Studio

Summary

This was the process of creating a ListView in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all