Hello World Application in Xamarin

First you must install Xamarin for Android.
 
Let's Start

1. Go to File -> New -> Project.

2.
 Select Android Application -> Give it a Name.

 

Before starting, have a look at Solution Explorer. An Android Application contains Properties, References, Assets and Resources and so on. Properties contain AssemblyInfo (the same as you saw in the C# project). The References folder contains Assembly Files (DLL files). The Mono.Android DLL assembly contains the C# binding to the Android API.

 

The resources folder contains a Drawable folder (that contains icons and images for the application), Layout (provides a designer surface for the application). Resource.Designer.cs contains auto generated code. As we will start from the basics, delete the main.axml layout and the Activity1.cs file from the project.

3. Right-click on Layout then seelct Add -> New Item.

 

4. Add an Android Layout (named Main.axml) then click on Add and select it.

 
 

5.
Drop a Button control from the Toolbox. 

 

Set the Button text as "Click Me!" and id as "@+id/btn_Hello".
 
Main.axml code: 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:minWidth="25px"  
  7.     android:minHeight="25px">  
  8.     <Button  
  9.         android:text="Click Me!"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/btn_Hello" />  
  13. </LinearLayout>  
6. Add a new item then select Activity then click on Add.

 
Activity1.cs Code: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Android.App;  
  7. using Android.Content;  
  8. using Android.OS;  
  9. using Android.Runtime;  
  10. using Android.Views;  
  11. using Android.Widget;  
  12.   
  13. namespace Hello_World_Application  
  14. {  
  15.     //Set the Label = "Hello World Application" which is displayed on the Top of the Application(in Title)  
  16.     //Set MainLauncher = true makes this Activity as a Main Activity and Application starts from this Activity. 
  17.     //Set Icon using Icon = "@drawable/icon"  
  18.     [Activity(Label = "Hello World Application", MainLauncher = true, Icon = "@drawable/icon")]  
  19.     public class Activity1 : Activity  
  20.     {  
  21.         //Creating Instance of Button  
  22.         Button btn_Hello;  
  23.         protected override void OnCreate(Bundle bundle)  
  24.         {  
  25.             base.OnCreate(bundle);  
  26.   
  27.             //SetContentView  
  28.             SetContentView(Resource.Layout.Main);  
  29.             //Get btn_Hello Button control from the Manin.axml Layout.  
  30.             btn_Hello = FindViewById<Button>(Resource.Id.btn_Hello);  
  31.             //Creating Click event of btn_Hello  
  32.             btn_Hello.Click += btn_Hello_Click;  
  33.         }  
  34.         //btn_Hello Click Event  
  35.         void btn_Hello_Click(object sender, EventArgs e)  
  36.         {  
  37.             //Display a Toast Message on Clicking the btn_Hello  
  38.             Toast.MakeText(this"Hello World Application by Anoop", ToastLength.Short).Show();  
  39.         }  
  40.     }  
  41. }  
7. Build and run the application. Start an emulator image if not started. Select the Running devices and then click on OK. 

Note
: the Emulator takes a lot of time to start. Enable USB Debugging on your Android Device and run your application on the Device directly; that is much faster compared to debugging on an emulator.

 

Final preview: 

 

I hope you like it. Thanks. 

Up Next
    Ebook Download
    View all
    Learn
    View all