Xamarin is a cross platform mobile application development with C# as shared codebase, developers can create native iOS, Android, and Windows apps using Xamarin tools. In this blog we will see how to write a simple Andriod Application using Xamarin in Visual Studio 2013. Before start building your application, you need to install Xamarin.Android in your development machine.
Please go through the following link to install Xamarin.Android.
Once the installation is successful, Start a new instance of Visual Studio 2013 and got to File-> New -> Project, where you can see the section Android under Visual C# Templates.
Select Blank App (Android) Template, provide a proper name and location and then Click OK. A default application is created in the solution explore with the following folder structure.
Lets have a look what is there inside.
- Resource folder contains all the resources we create in the application like icons, axml files and importantly all the id’s (identifiers) that we assign to a controls in the axml page. So whenever a developer wants to access a control through its id he has to use like the following
Where HelloWorldText is the id that is assigned to my TextView in my Main.axml file, which is explained below.
MainActivity.cs is nothing but a Page in windows world, so whenever you come across a file called activity then just remember it is a Page.
When u open the MainActivity.cs file you will find the following code
The MainActivity class contains an Attribute called Activity and has properties called Label, MainLauncher and Icon.
- Label property specified the name of the application and here the applications is called as ANDROID_APP.
- MainLauncher property specifies whether the current activity will be used as the Main Activity for the application, it is like the First page to be executed in the application.
- Icon property specifies the icon for the Application.
Before we look in to the class deeper, let’s create a simple control in the Main.axml file, this file contains xml markups for android.
Let’s create a TextView control inside the LinearLayout (linear layout is a layout that arranges its children in a single column or a single row), assign it with an id “HelloWorldText” and set the height, width and size property for the control.
Now let’s come back to the class MainActivity.cs. This class contains an OnCreate method which is override from the base class Activity. This method starts executing when the application starts running.
SetContentView is the method that takes the axml file as an input parameter and displays the file to the user. Initially I have said Resource class contains all the files and identifies used in the application. So whenever an axml file is added or a control is added in the axml file immediately build your project.
In our example we have a page called Main, so we are referring the file by Resources.Layout.Main
Next get the TextView identifier from the axml file. FindViewById takes identifier from the resource file as the parameter and returns the control.
var textView = this.FindViewById<TextView>(Resource.Id.HelloWorldText);
Set “Hello World” Text to the Text property of the textView.
textView.Text = "Hello World";
That’s it. Now you are ready to test your application.
Run the application by selecting the suitable Android Emulator Installed in the development machine.
Your Hello World Android App using Xamarin is up and Running.
Hope this will help you to get started with Xamarin.