Creating A Radio Button Control In Xamarin Android App Using Visual Studio 2015 Update Three

Introduction

Xamarin is a platform to develop the cross-platform and multi-platform apps. (Ex. Windows phone,android, ios). In the Xamarin platform, the  code sharing concept is used. Xamarin studio is available in Visual Studio also.

RadioButton control is called  if you create the form. Select the option (Ex: Gender Selection) and use the Radio Button Control.

Prerequisites

  • Visual Studio 2015 update 3

The following steps need to be followed in order to create RadioButton control in Xamarin Android app using visual studio 2015 update 3.

Step 1

Click file--> Select New--> Next select Project. 

or click (Ctrl+Shift+N)

visual studio.

Step 2

After opening the New Project Select Installed-->Templates-->Visual c#-->Android-->choose the Blank App (Android).

Next give your Android app a name(Ex:sample) and give the path of your project. Click ok.

visual studio.

Step 3

Next go to the solution explorer. Solution explorer has all the files and sources in your project.

Next Select Resource-->Layout-->double click to open main.axml page. You want select source to write the xaml code.

Choose the designer window so you can design your app.

visual studio.

Step 4

After opening, the main.axml file will open the main page designer. In this page choose which type you want so you can design this page.

visual studio.

Next you will Delete the Default hello world button. Go to the source panel you can see the button coding. you will delete it.

After deleting the xaml code next delete the c# button action code, go to the MainActivity.cs page. You will delete the button code.

Step 5

Next go to the toolbox window; it has all types of the tools and controls. You will go to the toolbox window; next scroll down

You will see the RadioButtonGroup control. You will drag and drop the RadioButtonGroup control in your main.axml page.



Step 6

After dragging and dropping the RadioButtonGroup control, next go to the properties windows change the Text value (Ex: android:text="Male") for all the radio buttons.



Step 7

Next Change the id of all radio buttons (EX: android:id="@+id/male") .



Code

Main.axml

  1. <RadioGroup android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioGroup1">  
  2.     <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Male" android:id="@+id/male" />  
  3.     <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" android:id="@+id/female" />  
  4.     <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Other" android:id="@+id/others" />  
  5. </RadioGroup>  
Step 8

Next go to the MainActivity.cs page write the c# code inside of OnCreate(Bundle bundle) Method, or copy the and paste the following code

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.         base.OnCreate(bundle);  
  3.         // Set our view from the "main" layout resource  
  4.         SetContentView(Resource.Layout.Main);  
  5.         RadioButton male = FindViewById < RadioButton > (Resource.Id.male);  
  6.         RadioButton female = FindViewById < RadioButton > (Resource.Id.female);  
  7.         RadioButton others = FindViewById < RadioButton > (Resource.Id.others);  
  8.         male.Click += RadioButtonClick;  
  9.         female.Click += RadioButtonClick;  
  10.         others.Click += RadioButtonClick;  
  11.     }  
  12.     //Next write the Method  
  13. private void RadioButtonClick(object sender, EventArgs e) {  
  14.     RadioButton rb = (RadioButton) sender;  
  15.     Toast.MakeText(this, rb.Text, ToastLength.Short).Show();  
  16. }  
visual studio.

Step 9

if you have android virtual device you can run the virtual device. if you don't have run your android phone, connect android phone via the USB cable in your system or laptop. If connecting the andoidd mobile phone it shows the message Allow USB debugging?.

If you always run the app in this mobile check the always allow from this computer. Next click the ok button. Next go to the visual studio.

If connecting your androidd phone show the run menu (Ex:Azus ZOOLD(Android 6.0-API 23)). if showing the phone in run option.

Next go to click the connected android phone it will run in your android phone.



Output

In this app running will  take some time because the app will be built and installed in your phone.
 
After running your app in your phone show the app with radiobutton control, if selecting Male radiobutton show the Male.



If selecting Female radiobutton show the Female.



Summery

So this was the process of creating RadioButton control in Xamarin Android app using visual studio 2015 update 3.

 

Up Next
    Ebook Download
    View all
    Learn
    View all