Creating An ImageButton 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 an ImageButton 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 design and you can 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.

Now, delete the Default hello world button.

Visual Studio

Go to the source panel and you can see the button coding. You need to delete it. After deleting XAML code, now delete C# button action code.

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

Step 5

In this step, add the Image form your Local system.

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

Visual Studio

Step 6

Now, you can choose the required image. Click Add.

Visual Studio

Step 7

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 ImageButton.

Visual Studio

Step 8

Now, go to the properties Window. You need to edit the ImageButton's Id Value and src Value (EX: android:id="@+id/ImageButton" android:src="@drawable/xamarin").

Visual Studio

Step 9

In this step, go to the Main.axml page source panel. Note the ImageButton'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.     <ImageButton android:src="@drawable/xamarin" android:layout_width="125.0dp" android:layout_height="111.5dp" android:id="@+id/ImageButton" android:layout_marginRight="0.0dp" android:layout_gravity="center" /> </LinearLayout>  
Visual Studio

Step 10

In this step, go to MainActivity.cs page and write the code, mentioned below, in OnCreate() Method.

MainActivity.cs

  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     var button = FindViewById < ImageButton > (Resource.Id.ImageButton);  
  6.     button.Click += delegate {  
  7.         Console.WriteLine("button clicked");  
  8.     };  
  9. }  
Visual Studio

Step 11

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 Run option.

Visual Studio

Output

After few seconds, the app will start running on your phone. You will see that the ImageButton is working successfully.

Visual Studio

Summary

This was the process of how to create an ImageButton in Xamarin Android app, using Visual Studio 2015.


Similar Articles