Introduction

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. using Android.App;
    3. using Android.Content;
    4. using Android.Runtime;
    5. using Android.Views;
    6. using Android.Widget;
    7. using Android.OS;
    8. namespace CheckBoxDemo
    9. {
    10. [Activity(Label = "CheckBoxDemo", MainLauncher = true, Icon = "@drawable/icon")]
    11. public class Activity1 : Activity
    12. {
    13. //Create instance of Button
    14. Button btn_Submit;
    15. protected override void OnCreate(Bundle bundle)
    16. {
    17. base.OnCreate(bundle);
    18. //setting the view for the Activity
    19. SetContentView(Resource.Layout.Main);
    20. //Get btn_Submit Button and checkBox1 CheckBox control from the Main.xaml Layout.
    21. btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
    22. CheckBox checkBox1 = FindViewById<CheckBox>(Resource.Id.checkBox1);
    23. //CheckBox CheckedChange Event
    24. checkBox1.CheckedChange += checkBox1_CheckedChange;
    25. //btn_Submit Click Event
    26. btn_Submit.Click += btn_Submit_Click;
    27. }
    28. void checkBox1_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
    29. {
    30. //If CheckBox is Checked
    31. if(e.IsChecked==true)
    32. {
    33. btn_Submit.Enabled = true;
    34. }
    35. else
    36. {
    37. btn_Submit.Enabled = false;
    38. }
    39. }
    40. void btn_Submit_Click(object sender, EventArgs e)
    41. {
    42. Toast.MakeText(this,"Thanks for accepting Terms and Condition",ToastLength.Short).Show();
    43. }
    44. }
    45. }
    l Preview
    I hope you like it. Thanks.