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
Go to File > New > Project and create project.
Step 2
In Solution Explorer (Ctrl + Alt + L), you can now see the default structure.
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.
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.
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.
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.
- using Android.App;
- using Android.Widget;
- using Android.OS;
-
- namespace RadioButtonDemo
- {
- [Activity(Label = "RadioButtonDemo", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView(Resource.Layout.Main);
- }
- }
- }
Step 7
Add the code line given below after SetContentView(Resource.Layout.Main),
- Button btnSelection = FindViewById<Button>(Resource.Id.btnSelection);
- 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).
- void btnSelection_Click(object sender, System.EventArgs e)
-
- {
- RadioGroup rbSelection = FindViewById<RadioGroup>(Resource.Id.rbSelection);
- RadioButton rbSelectedOpt = FindViewById<RadioButton>(rbSelection.CheckedRadioButtonId);
- var alert = new AlertDialog.Builder(this);
- alert.SetMessage("Your choice is : " + rbSelectedOpt.Text).Show();
- }
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.
- using Android.App;
- using Android.Widget;
- using Android.OS;
-
- namespace RadioButtonDemo
- {
- [Activity(Label = "RadioButtonDemo", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView(Resource.Layout.Main);
-
- Button btnSelection = FindViewById<Button>(Resource.Id.btnSelection);
- btnSelection.Click += btnSelection_Click;
- }
-
- void btnSelection_Click(object sender, System.EventArgs e)
- {
- RadioGroup rbSelection = FindViewById<RadioGroup>(Resource.Id.rbSelection);
- RadioButton rbSelectedOpt = FindViewById<RadioButton>(rbSelection.CheckedRadioButtonId);
- var alert = new AlertDialog.Builder(this);
- alert.SetMessage("Your choice is : " + rbSelectedOpt.Text).Show();
- }
- }
- }
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.
Output
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.