Xamarin Tutorial
Xamarin Tutorial
Xamarin is built on the .NET Framework. It allows one to create apps that easily run across
multiple platforms. In this tutorial, we will explain how you can use Xamarin to deliver
native iOS, Android, and Windows Apps.
Audience
This tutorial has been developed for beginners to help them understand the basics of
creating native Apps using Xamarin.
Prerequisites
All the programs in this tutorial have been developed using Visual C#. Therefore, you
should have a good understanding of code written in C# programming language.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com.
i
Xamarin
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
TextView ............................................................................................................................................... 21
Button ................................................................................................................................................... 21
ii
Xamarin
9. XAMARIN ─ LAYOUTS......................................................................................................... 38
Linear Layout......................................................................................................................................... 38
Table Layout.......................................................................................................................................... 44
Spinner .................................................................................................................................................. 51
ListViews ............................................................................................................................................... 59
GridViews.............................................................................................................................................. 60
iii
1. Xamarin ─ Installation Xamarin
Xamarin is built on the .NET Framework. It allows one to create apps that easily run across
multiple platforms. In this tutorial, we will explain how you can use Xamarin to deliver
native iOS, Android, and Windows Apps.
Let’s start the tutorial with a discussion on how to install Xamarin in Windows and Mac
systems.
System Requirements
Windows
A computer with at least 2GB of RAM and running Windows 7 or higher (Windows
8-10 is highly recommended)
Mac
A Mac computer running OS X Yosemite (10.10) or higher
Xamarin iOS SDK
Apple’s Xcode (7+) IDE and iOS SDK
Xamarin Studio
Installation on Windows
Download the Xamarin Installer from http://www.xamarin.com/download. Before running
the Xamarin installer, make sure you have installed Android SDK and Java SDK on your
computer.
The Xamarin license agreement screen appears. Click the Next button to accept
the agreement.
The installer will search for any missing components and prompt you to download
and install them.
After the Xamarin installation is complete, click the Close button to exit and get
ready to start using Xamarin.
1
Xamarin
Installation on Mac
Download the Xamarin Studio Installer on your Mac system.
Run the Xamarin installer you downloaded and follow the steps given in the
Installation Wizard.
After the installation is complete, you can start using Xamarin on your system.
2
2. Xamarin ─ First Application Xamarin
In this chapter, we will see how to create a small Android application using Xamarin.
3
Xamarin
On the Menu dialog box that appears, go to Templates -> Visual C# -> Android ->
Blank App (Android).
Give an appropriate name for your application. In our case, we name it “helloWorld” and
save it in the default location provided. Next, click the OK button for the new
“helloXamarin” project to load.
On the solution, open Resources -> layout -> Main.axml file. Switch from Design
View and go to the Source file and type the following lines of code to build your app.
4
Xamarin
In the above code, we have created a new Android textview. Next, open the folder values
and double-click Strings.xml to open it. Here, we are going to store information and
values about the button created above.
Open MainActivity.cs file and replace the existing code with the following lines of code.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin
{
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
}
}
}
5
Xamarin
Save the application. Build and then run it to display the created app in an Android
Emulator.
If you do not have an Android Emulator, then follow the steps given in the next section to
create one.
6
Xamarin
On the above screen, supply the AVD name you want. Select a device that is appropriate
for your display, e.g., Nexus 4” display. Select your target platform. It is always
advisable to test on a minimum target platform, e.g., API 10 Android 2.3 (Gingerbread)
so as to ensure your App works across all Android platforms.
Fill in the rest of the fields and click the OK button. Your emulator is now ready. You can
select it from the list of existing Android Virtual Devices and then click Start to launch it.
7
Xamarin
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ButtonClick" />
After adding a button, our full code will look like this:
8
Xamarin
After adding our button in the strings.xml file, we will open MainActivity.cs file to add
an action for our button when it is clicked, as shown in the following code.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin
{
[Activity(Label = "HelloXamarin", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = "Hello world I am your
first App"; };
}
}
}
9
Xamarin
After clicking on the button, you will get the following output:
10
3. Xamarin ─ Application Manifest Xamarin
Here, we have listed down some of the important functions of a manifest file:
It declares the permissions required by the application, e.g., camera, location, etc.
11
Xamarin
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" >
Version Name: It is a user-friendly version string for your App that users will see on your
App settings and on the Google PlayStore. The following code shows an example of a
version name.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionName="1.0.0">
Minimum Android Version: It is the lowest Android version platform which your application
supports.
12
Xamarin
In the above example, our minimum Android version is API Level 16, commonly referred
to as JELLY BEAN.
Target Android Version: It is the Android version on which your App is compiled against.
13
4. Xamarin ─ Android Resources Xamarin
When a new Android project is created, there are some files that are added to the project,
by default. We call these default project files and folders as Android Resources. Take a
look at the following screenshot.
Resources folder: Resources can be images, layouts, strings, etc. that can be loaded
via Android’s resource system.
Resources/drawable folder: It stores all the images that you are going to use in your
application.
Resources/layout folder: It contains all the Android XML files (.axml) that Android
uses to build user interfaces.
14
Xamarin
MainActivity.cs file: This is the first activity of your Android application and from
where the main application actions are launched from.
The following code shows how to create a gridview project containing seven images.
namespace HelloGridView
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tas
ks", "1.0.0.0")]
public partial class Resource
{
static Resource()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
15
Xamarin
static Drawable()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
private Drawable() { }
}
static Id()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
private Id() { }
16
Xamarin
static String()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
private String() { }
}
}
}
From the above code, the seven images are referenced in a class called drawable. These
images are added programmatically. If a user adds another image to the project, it will
also be added to the drawable class. The gridview contained in the project is also added
and stored in a class on its own. Each item contained in the resources folder is
automatically generated and stored in a class.
17
Xamarin
18