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)
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.
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.
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.
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
- <RadioGroup android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioGroup1">
- <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Male" android:id="@+id/male" />
- <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" android:id="@+id/female" />
- <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Other" android:id="@+id/others" />
- </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 - protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- RadioButton male = FindViewById < RadioButton > (Resource.Id.male);
- RadioButton female = FindViewById < RadioButton > (Resource.Id.female);
- RadioButton others = FindViewById < RadioButton > (Resource.Id.others);
- male.Click += RadioButtonClick;
- female.Click += RadioButtonClick;
- others.Click += RadioButtonClick;
- }
- //Next write the Method
- private void RadioButtonClick(object sender, EventArgs e) {
- RadioButton rb = (RadioButton) sender;
- Toast.MakeText(this, rb.Text, ToastLength.Short).Show();
- }
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.