Xamarin Android RadioButton and RadioGroup Introduction and Example
A radio button is a two-states button that can be either checked or unchecked.
These two states button are also normally generally called Compound buttons.
And indeed they derive from
Android.Widget.CompoundButton, an abstract class that acts as an interface definition for a callback to be invoked when the checked state of a compound button changed.
So in a way a radiobutton share a similarity with CheckBox by deriving from the
However, one big difference with CheckBox is that a RadioButton, unlike a CheckBox cannot be unchecked when it’s already checked.
Say you have two radiobuttons: one maleRadioButton and the other femaleRadioButton. If you check male you expect female to be unchecked obviously.
However, if you click male twice, don’t expect it to get unchecked. You have to move over to the female and then given that we have them in a RadioGroup, the female gets checked while the male gets unchecked.
You can check our video tutorial here:
RadioButton is defined in the
That namespace belongs to the
RadioButton is a concrete class that derives
public class RadioButton : Android.Widget.CompoundButton
On it’s own RadioButton doesn’t have any public method or public properties.
Instead it gets them via Inheritance from the
Important Properties and Methods
Here are some of the important properties of RadioButton
||Property||Get or Set checked state of the button.This actually defined in the
||Method||Changes the checked state of the view to the inverse of its current state. Also defined in the
||Event||Raised when the checked state of the RadioButton is detected.Also defined in the
Just the similar properties, events and methods that CheckBox gets.
On their own, it would be difficult to juggle the states of many RadioButtons. This is because normally you expect only one radio button to be checked at any givem time.
This forces users to select only one item.
So there is a class called
RadioGroup defined in the
Android.Widget namespace that allows us easily work with many RadioButtons.
This is because when you add your RadioButtons inside a RadioGroup, then checking one radio button unchecks all the others.
RadioGroup derives from
public class RadioGroup : Android.Widget.LinearLayout
RadioGroup Important Properties,Methods and Events
Here are some important properties, methods and events for the RadioGroup class:
||Property||Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.|
||Method||Sets the selection to the radio button whose identifier is passed in parameter. Using -1 as the selection identifier clears the selection; such an operation is equivalent to invoking
||Method||Clears the selection. When the selection is cleared, no radio button in this group is selected and
||Event||Raised when the checked state of the radiobuttons in the group changes.|
Example – Creating RadioButtons and RadioGroup Programmatically without XML
We’ll now see an example. First we will programmatically instantiate several RadioButtons and one radiogroup, add the radiobutton in the radiogroup, and listen to checkChange events thus showing a Toast message with selected items it.
Creating RadioGroup and RadioButtons with XML, then getting Text from Selected RadioButton
In this example we will see how to add the RadioGroup in our XML layout, add the RadioButtons to it, then in our C# code, when a radioButton is selected, show it’s selected Text in a Toast message.
Here’s our XML Layout code:
Here’s our MainActivity