Before we start, let us take a brief look at what Xamarin is. It is a tool provided by Microsoft to build native iOS and Android applications using C#. It is built on the .NET framework and allows to build applications that can share the source code and run on multiple platforms. This article is directed towards beginners who are getting started with Xamarin development.
The origin of Xamarin goes back in 1999 when Miguel de Icaza and Nat Friedman launched a project which was called Ximian previously. At that time, it was acquired by Novell which was further acquired by Attachmate. The final acquisition resulted in laying off developers and closing down the project. The complete formation of Xamarin was announced by Miguel in 2011. Xamarin comes from the name of Tamarin monkey, replacing the first letter T with X.
Xamarin was created as a commercial project and got acquired by Microsoft in 2016. It already has a community of 1.4 million developers from all over the world.
Getting Started with Our Application
To get started, regardless of the operating system you use, you have to install Visual Studio in order to proceed with this tutorial. Visual Studio is a necessary tool or you can say the developing environment to get started with Xamarin. I am using Visual Studio Community Edition, which is free of cost. After you have installed the IDE, please follow next step.
To create a new application we have to select Android after launching the IDE installed. You can find the label Android under Visual C#. Choose the blank template that says: Blank App (Android). This option allows us to build a native Android application.
To have this option available, you must have Java SDK and Android SDK installed in your development environment.
Once the project is generated, you will notice on the right hand side there is a tab called Solution Explorer. This is our project navigator that shows all files and folders. Open the MainActivity.cs file from it. In this file we are going to handle our code.
This file includes some boilerplate code. To define the layout of our application we need to find the file Main.axml. This file is inside the folder name Resources and layout. This file is equivalent to activity_main.xml if you are already familiar with Android application development.
Open that file and you will see that we can now define views for our layout and other graphical elements if needed. There is a toolbox provided by the IDE on the left for adding things visually such as a Text View, buttons, images, etc. For our basic application, we are going to add the source ourself. Click on the Source tab and add the following code.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:minWidth="25px" android:minHeight="25px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView1" /> </LinearLayout>
The component allows us to define how various elements will be arranged on the screen. It is provided to us by default. In the above snippet of code, we just add a new text class called TextView with text Hello World! In it.
TextView Class displays the text to the user of the application. LinearLayout arranges its children, in our case the TextView, either in a single column or a single row. The direction of the row can be set by calling LinearLayout.Orientation.
Xamarin’s API has complete support for interacting and building components using Android and iOS just like in native development. The advantage Xamarin provides is that it makes the development process for building cross-platform application robust and provides some amount of familiarity with the tools used to develop an application such as an IDE like Visual Studio.
Benefits of using Xamarin
Since we have successfully develop a simple application, let us take a look at what are the benefits of Xamarin and why you should consider using it for your next mobile application.
Single Stack for Cross Platform Development
To create mobile applications for either platform such as iOS and Android Xamarin uses C# with .NET framework. This certainly means that you can share or reuse 80% of the source code. This speeds up the development process and there is not need to switch development environment while building cross platform applications and also reduces the cost and time as a resource for building applications on both platforms.
Cross-platform app built with Xamarin will be closer to a Native application depending on the use case, in terms of performance. Unlike other hybrid cross-platform solutions, Xamarin is not using web views for user interface and interaction.
Xamarin applications are easy to maintain and update. In this process, a developer can deploy the changes or updates to the source code and these new changes will be applied to both iOS and Android applications. There is a requirement for this process to happen that as Xamarin developer you need to take care and must use Xamarin.Forms.
Simplified Development Ecosystem
Xamarin development environment consists of Xamarin IDE, the Visual Studio, Xamarin SDK, Xamarin Test Cloud for testing, and Xamarin.Insights for analytics. That is the complete requirement for engaging and investing with Xamarin. There is no need to invest time and other resources in figuring out how to achieve same results with third party application to test, and deploy your application.
Native Level Functionality
To access platform specific libraries, Xamarin provides access to these native libraries by linking. This eliminates common hardware problems such as compatibility issues, and a development team can make use of certain plugins that are available in native APIs.
Can build applications for Mac
Xamarin had a new level of API added that allows to develop native Mac OS X applications in C# and .NET Framework. It allows linking and access to same set of APIs that are used to develop Mac applications using Objective-C. This is huge benefit since the application source code now can be shared between across multiple platforms such as Android, iOS and Mac OS. You can use Xamarin.Mac with Xcode to use Xcode’s Interface builder for developing desktop applications.