Create Your First Android App Using Xamarin And Visual Studio 2013

Introduction

Now, you can create mobile apps with C#. Xamarin allows us to create an Android, iOS, Windows mobile Application with native user interface and shared code across multiple platforms.

This article demonstrates how to create your first Android Application with Xamarin. Here, we will take RadioButton group and button, where on the click of button, we will alert our radio button selection. I am creating this demo, using Visual Studio 2013, which is the minimum requirement to create Xamarin app.

Step 1

android

Go to File > New > Project and create project.

Step 2

In Solution Explorer (Ctrl + Alt + L), you can now see the default structure.

android

Main.axml - This is the file, where we will create our UI.

MainActivity.cs - In this file, we will write the code for our UI.

Step 3

Open Main.axml file and from toolbox (Ctrl + Alt + X), drag RadioGroup and button to your layout.

android

android

Step 4

Now, change the id of the controls; which you have dragged, as it is preferable to have a meaningful id for a control. For this, select control (RadioGroup) and press F4. Here, properties Window will appear.

android

In the similar way, you can change the id and text for button control.

Step 5

Change the text and an id for Radio button under RadioGroup. For this, select single Radio button and press F4(properties Window), as shown below.

android

In a similar way, change the id and the text for all three-radio buttons under RadioGroup.

Step 6

Now, open MainActivity.cs file will be, as shown below. 

  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4.   
  5. namespace RadioButtonDemo  
  6. {  
  7.     [Activity(Label = "RadioButtonDemo", MainLauncher = true, Icon = "@drawable/icon")]  
  8.     public class MainActivity : Activity  
  9.     {  
  10.         protected override void OnCreate(Bundle bundle)  
  11.         {  
  12.             base.OnCreate(bundle);  
  13.   
  14.             // Set our view from the "main" layout resource  
  15.             SetContentView(Resource.Layout.Main);  
  16.         }  
  17.     }  
  18. }   

Step 7

Add the code line given below after SetContentView(Resource.Layout.Main), 

  1. Button btnSelection = FindViewById<Button>(Resource.Id.btnSelection);  
  2. btnSelection.Click += btnSelection_Click;   

Here, we are finding a button from our layout and creating a click event for the button.

Step 8

Add the code given below after the completion of OnCreate(Bundle bundle). 

  1. void btnSelection_Click(object sender, System.EventArgs e)  
  2.   
  3. {  
  4.  RadioGroup rbSelection = FindViewById<RadioGroup>(Resource.Id.rbSelection);  
  5.  RadioButton rbSelectedOpt = FindViewById<RadioButton>(rbSelection.CheckedRadioButtonId);  
  6.  var alert = new AlertDialog.Builder(this);  
  7.  alert.SetMessage("Your choice is : " + rbSelectedOpt.Text).Show();  
  8. }   

Line 1

We are finding RadioGroup from our layout

Line 2

Here, we are finding CheckedRadioButtonId from radio group, which we have found in previous line

Line 3

We are creating an alert box to be called on click of our button.

Line 4

Here, we are setting and displaying an alert message on our button click, where we will display our selected radio button text.

When we will complete writing the code given above, our MainActivity.cs file will look, as shown below.

  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4.   
  5. namespace RadioButtonDemo  
  6. {  
  7.     [Activity(Label = "RadioButtonDemo", MainLauncher = true, Icon = "@drawable/icon")]  
  8.     public class MainActivity : Activity  
  9.     {  
  10.         protected override void OnCreate(Bundle bundle)  
  11.         {  
  12.             base.OnCreate(bundle);  
  13.   
  14.             // Set our view from the "main" layout resource  
  15.             SetContentView(Resource.Layout.Main);  
  16.   
  17.             Button btnSelection = FindViewById<Button>(Resource.Id.btnSelection);  
  18.             btnSelection.Click += btnSelection_Click;  
  19.         }  
  20.   
  21.         void btnSelection_Click(object sender, System.EventArgs e)  
  22.         {  
  23.             RadioGroup rbSelection = FindViewById<RadioGroup>(Resource.Id.rbSelection);  
  24.             RadioButton rbSelectedOpt = FindViewById<RadioButton>(rbSelection.CheckedRadioButtonId);  
  25.             var alert = new AlertDialog.Builder(this);  
  26.             alert.SetMessage("Your choice is : " + rbSelectedOpt.Text).Show();  
  27.         }  
  28.     }  
  29. }   

Step 9

Now, you are ready to run your first Android Application. For this, first build your Application by pressing (Ctrl + Shift + B), followed by running the Application.

android

Output

android  

Summary

This is only an Android Application, using Xamarin. To create an Application for all the other platforms, we need to create our project, using cross-platform template in create new project Window.

You can download Xamarin.

Up Next
    Ebook Download
    View all
    Learn
    View all