CheckBox Widget in Xamarin

In this article, we will see how to use a CheckBox widget control and the CheckBox's CheckedChange event in Xamarin. For demonstration, I will create a simple application in which if the user accepts the terms and conditions (using the CheckBox) then the Submit button becomes enabled. If you are new to this Series on Xamarin then I suggest you read my previous articles of this series.
Let's Begin.

1. Create a new Android Application.

 

2.
Drop a CheckBox (with id="@+id/checkBox1") and Button (with id="@+id/btn_Submit") control onto the Layout, Main.axml.

 
Set the text of the CheckBox as Accept Terms and Condition and the Button's text as Submit.

 

Main.axml source code: 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <CheckBox  
  7.         android:text="Accept Terms and Conditions"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/checkBox1" />  
  11.     <Button  
  12.         android:text="Submit"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/btn_Submit"  
  16.         android:enabled="false" />  
  17. </LinearLayout>  
In Activity1.cs, we have created a CheckBox CheckedChange event and btn_Submit Click event. The following is the Activity1.cs code: 
  1. using System;  
  2.   
  3. using Android.App;  
  4. using Android.Content;  
  5. using Android.Runtime;  
  6. using Android.Views;  
  7. using Android.Widget;  
  8. using Android.OS;  
  9.   
  10. namespace CheckBoxDemo  
  11. {  
  12.     [Activity(Label = "CheckBoxDemo", MainLauncher = true, Icon = "@drawable/icon")]  
  13.     public class Activity1 : Activity  
  14.     {  
  15.         //Create instance of Button  
  16.         Button btn_Submit;  
  17.         protected override void OnCreate(Bundle bundle)  
  18.         {  
  19.             base.OnCreate(bundle);  
  20.             //setting the view for the Activity    
  21.             SetContentView(Resource.Layout.Main);  
  22.             //Get btn_Submit Button and checkBox1 CheckBox control from the Main.xaml Layout.  
  23.             btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);  
  24.             CheckBox checkBox1 = FindViewById<CheckBox>(Resource.Id.checkBox1);  
  25.             //CheckBox CheckedChange Event  
  26.             checkBox1.CheckedChange += checkBox1_CheckedChange;  
  27.             //btn_Submit Click Event  
  28.             btn_Submit.Click += btn_Submit_Click;  
  29.         }  
  30.   
  31.         void checkBox1_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)  
  32.         {  
  33.             //If CheckBox is Checked  
  34.             if(e.IsChecked==true)  
  35.             {  
  36.                 btn_Submit.Enabled = true;  
  37.             }  
  38.             else  
  39.             {  
  40.                 btn_Submit.Enabled = false;  
  41.             }  
  42.         }  
  43.   
  44.         void btn_Submit_Click(object sender, EventArgs e)  
  45.         {  
  46.             Toast.MakeText(this,"Thanks for accepting Terms and Condition",ToastLength.Short).Show();  
  47.         }  
  48.     }  
  49. }  
Final Preview: 

 

I hope you like it. Thanks.