Image Gallery 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, mentioned below, are required to be followed in order to create the Image Gallery View 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).



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 the path of your project. Afterwards, click OK.

Xamarin

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Now, select Resource-->Layout-->double click to open main.axml page. If you want to select source, write XAML code.

Choose the Designer Window, if you want to design so you can design your app.

Xamarin

Step 4

After opening the main.axml file will open the main page designer. You can design the page, if you want.

Xamarin

Now, delete the Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

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

You need to drag and drop the Gallery.


Xamarin

Step 6

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

Xamarin

Step 7

In this step, go to the Main.axml page Source Panel. Note, the Gallery Id value.

Xamarin

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.     <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" />  
  3. </LinearLayout>  
Step 8

In this step, add the Images form your local system.

Go to Solution Explorer-->Resource-->Drawable-->Right Click-->Add-->Exsisting Item (Shift+Alt+A).

Xamarin

Step 9

Now you can select the desired images and click Add.

Xamarin

Step 10

In this step, add one class file and its name is ImageAdapter.

Go to Solution Explorer-->Resource-->Right Click-->Add-->New Item (Ctrl+shift+A).

Xamarin

Step 11

Now, select the class file, give the name ImageAdapter.cs and click add.

Xamarin

Step 12

After creating the ImageAdapter.cs, write one sub class, whose name is BaseAdapter and create one variable context.

Xamarin

Step 13

Now, write the code, mentioned below from ImageAdapter.cs class file.

ImageAdapter.cs
  1. class ImageAdapter: BaseAdapter {  
  2.     Context context;  
  3.     public ImageAdapter(Context c) {  
  4.         context = c;  
  5.     }  
  6.     public override int Count {  
  7.         get {  
  8.             return thumbIds.Length;  
  9.         }  
  10.     }  
  11.     public override Java.Lang.Object GetItem(int position) {  
  12.         return null;  
  13.     }  
  14.     public override long GetItemId(int position) {  
  15.             return 0;  
  16.         }  
  17.         // create a new ImageView for each item referenced by the Adapter  
  18.     public override View GetView(int position, View convertView, ViewGroup parent) {  
  19.             ImageView i = new ImageView(context);  
  20.             i.SetImageResource(thumbIds[position]);  
  21.             i.LayoutParameters = new Gallery.LayoutParams(400, 400);  
  22.             i.SetScaleType(ImageView.ScaleType.FitXy);  
  23.             return i;  
  24.         }  
  25.         // references to our images  
  26.     int[] thumbIds = {  
  27.         Resource.Drawable.image1,  
  28.         Resource.Drawable.image2,  
  29.         Resource.Drawable.image3,  
  30.         Resource.Drawable.image4,  
  31.         Resource.Drawable.image5,  
  32.         Resource.Drawable.image6,  
  33.         Resource.Drawable.image7  
  34.     };  
  35. }  
Xamarin

Step 14

Now, go to the MainActivity.cs page. Write the code, mentioned below from OnCreate() Method.

MainActivity.cs
  1. Gallery gallery = (Gallery) FindViewById < Gallery > (Resource.Id.gallery);  
  2. gallery.Adapter = new ImageAdapter(this);  
  3. gallery.ItemClick += delegate(object sender, Android.Widget.AdapterView.ItemClickEventArgs args) {  
  4.     Toast.MakeText(this, args.Position.ToString(), ToastLength.Short).Show();  
  5. };  
Xamarin

Step 15

If you have 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.

Xamarin

Output

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

You will see the Image Gallery view successfully.

Xamarin

Slide to change the images.

Xamarin

Summary

This was the process to create the Image Gallery View in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all