A CheckBox is type of a CompoundButton that has two states: Checked or UnChecked.
CheckBoxes are a popular way of representing Boolean values in Forms. Not only do they get used in mobile applications but also in desktop as well as online forms.
A CheckBox is a CompoundButton in that it has two states. Also it does derive from the Android.Widget.CompoundButton
class.

Xamarin Android Checkbox
The CompoundButton
is an abstract class defined in the Android.Widget
package that acts as the interface definition for a callback to be invoked when the checked state of its child is changed.
The CompoundButton itself is a Button so CheckBox itself will inherit capabilities for Butttons.
CheckBox Definition
The CheckBox resides in the Android.Widget
namespace.
namespace Android.Widget
It derives from Android.Widget.CompoundButton
:
public class CheckBox : Android.Widget.CompoundButton
The namespace resides in the Mono.Android
assembly.
Important Properties and Methods
Here are some of the important properties of CheckBox
Property | Type | Description |
---|---|---|
public virtual bool Checked { set; get; } |
Property | Get or Set checked state of the button.This actually defined in the CompoundButton class. |
public virtual void Toggle() |
Method | Changes the checked state of the view to the inverse of its current state. Also defined in the CompoundButton class. |
public event System.EventHandler<CompoundButton.CheckedChangeEventArgs> CheckedChange |
Event | Raised when the checked state of the CheckBox is detected.Also defined in the CompoundButton class. |
Example
We will have an Example that will change the CheckBox Text property value based on whether our CheckBox is checked or not.
using Android.App;
using Android.Widget;
using Android.OS;
namespace XamCheckBoxHello
{
[Activity(Label = "XamCheckBoxHello", MainLauncher = true, Theme = "@android:style/android:Theme.Holo.Light.DarkActionBar", Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private CheckBox myCheckBox;
/*
* When activity is Created.
*/
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
//reference checkbox
myCheckBox = FindViewById<CheckBox>(Resource.Id.myCheckbox);
//register event handler
myCheckBox.CheckedChange += myCheckBox_CheckedChange;
}
/*
* CheckBox State change event handler
*/
void myCheckBox_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
myCheckBox.Text = e.IsChecked ? "CheckBox Checked" : "CheckBox unchecked";
}
}
}
Main.axml
This is our layout. We will add our CheckBox as well as a header label which is a TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/headerLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="casual"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="CheckBox"
android:textAllCaps="true"
android:textSize="24sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/myCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="CheckBox unchecked"
android:padding="5dp" />
</RelativeLayout>
NOTES
- We import namespaces to use into our project using the
using
directives. - Then we define our namespace to host our class.
- Then we register properties of application that normally get registered in the
androidmanifest.xml
. In this case we register them as attributes to our class. First we have the Label for application, then set this activity as the launcher activity of our app. Then we the theme to use in our xamarin android app via the Theme attribute. Instead of using the default ugly dark theme, we will change the theme to light holo theme with dark actionbar:Theme = "@android:style/android:Theme.Holo.Light.DarkActionBar"
. - Then set make our class derive from Activity.
- We’ll have one instance field of type CheckBox called
myCheckBox
. - We then override the
onCreate()
method of our activity. - We set the Content view using the SetContentView(), passing in the layout to be inflated.
- We then reference the CheckBox from XML layout using FindViewById.
- Then listen to CheckedChange events for our CheckBox.
- Then define the event handler. In this case we will be changing the Text property of the CheckBox.
Good day.