ImageView In Xamarin Native Platform With Example

Introduction

Reading this article, you will learn how to create a ImageView application in Xamarin for Android platform.

Tools

  1. Windows 10 (Recommended)
  2. Visual Studio 2017 Community/Enterprise/Professional Edition (Software available online https://www.visualstudio.com/downloads/)

Step 1

Go to Visual Studio 2017.

Click File —> New —> Project or shortcuts (Ctrl+Shift+N).

Step 2

Go to Visual C# --> Android –> Blank App (Android); change your application name and click the "OK" button.

 

Step 3

After the process completes, the Xamarin dashboard appears. At the top right, you will see Solution Explorer. There is your application solution. Select application name; extract to resources. The Layout folder appears in which there is Main.axml* file. Double click to open Main.axml file. Then double click on down side left corner appear Source button. Don’t change android version default V23.

 

Step4

Next, double click on Source button. Then XAML code appears, if you need code.

 

Step5

I have given the source code. You can use this code or you can use your own code.

 

AXML Code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.       android:orientation="vertical"  
  5.       android:layout_width="match_parent"  
  6.       android:layout_height="match_parent">  
  7.     <TextView  
  8.          android:id="@+id/txtView"  
  9.          android:layout_width="wrap_content"  
  10.          android:layout_height="wrap_content"  
  11.          android:text="Android ImageView Example"  
  12.          android:textSize="22sp"  
  13.          android:textStyle="bold" />  
  14.     <ImageView  
  15.          android:id="@+id/imageView"  
  16.          android:layout_width="wrap_content"  
  17.          android:layout_height="300dp"  
  18.          android:src="@drawable/Icon" />  
  19.     <Button  
  20.          android:text="Previous"  
  21.          android:layout_width="match_parent"  
  22.          android:layout_height="wrap_content"  
  23.          android:id="@+id/btnPre" />  
  24.     <Button  
  25.          android:text="Next"  
  26.          android:layout_width="match_parent"  
  27.          android:layout_height="wrap_content"  
  28.          android:id="@+id/btnNext" />  
  29. </LinearLayout>  

Step6

Then, let's go to the Solution Explorer, click on MainActivity.cs. Your application name could be that namespace.

 

I have given the source code. You can use this code or you can use your own code.

 

SOURCE CODE
  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using Android;  
  9. namespace ImageViewApp {  
  10.     [Activity(Label = "AndroidImageView", MainLauncher = true, Icon = "@drawable/icon")]  
  11.     public class MainActivity: Activity create //classname   
  12.     {  
  13.         protected override void OnCreate(Bundle bundle) {  
  14.             base.OnCreate(bundle);  
  15.             // Set our view from the "main" layout resource  
  16.             SetContentView(Resource.Layout.Main);  
  17.             //  
  18.             var imageView = FindViewById < ImageView > (Resource.Id.imageView);  
  19.             var btnPre = FindViewById < Button > (Resource.Id.btnPre); //Button performance  
  20.             var btnNext = FindViewById < Button > (Resource.Id.btnNext);  
  21.             btnPre.Click += (e, o) => {  
  22.                 imageView.SetImageResource(Resource.Drawable.Icon);  
  23.                 btnNext.Visibility = ViewStates.Visible;  
  24.                 btnPre.Visibility = ViewStates.Invisible;  
  25.             };  
  26.             btnNext.Click += (e, o) => { // Conditions  
  27.                 imageView.SetImageResource(Resource.Drawable.Android);  
  28.                 btnPre.Visibility = ViewStates.Visible;  
  29.                 btnNext.Visibility = ViewStates.Invisible;  
  30.             };  
  31.         }  
  32.     }  
  33. }  

Just create a page application name, Simple android image and two button. This is only for demo app; you can use more codes using design in this application.

Step7

Then, build this application; wait for a few seconds; all calling functions will get connected. Next build successful and not failed; 0 up-to-date;0 skipped.

 

Step 8

Select a device; I have used emulator only so I selected “5’ KitKat (4.4)XXHDPI Application program interface (API) 19”.

 

Step 9

Emulator screen appears but this emulator needs 2GB RAM so you need minimum 4GB RAM your machine along with an i3 3rd Generation processor. You can use Image View.

 

Step 10

Click on Previous Button Image size is normal.

 

Step 11

Click on Next Button Image Size is Very big or Zoom Image.

 

SUMMARY

In this article, you learned how to create an Image View app using Xamarin Android native application development feature of Xamarin with .NET Standard libraries.

If you have any questions/ feedback/ issues, please write in the comment box.

Next Recommended Readings