In this Article, We will see How to use CheckBox widget control and CheckBox's CheckedChange Event in Xamarin.For Demonstration, I am going to create a Simple application in which if user accept the terms and condition(using CheckBox) then Submit button become enabled. Before starting, 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 CheckBox as Accept Terms and Condition and Button's Text as Submit.
Main.axml Code:
In Activity1.cs, We have created CheckBox CheckedChange Event. The following is Activity1.cs Code:
Final Preview:
Hope you like it. Thanks.
- Hello World Application in Xamarin
- Creating Splash Screen for Android App in Xamarin.
- Auto Complete Text View in Xamarin
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 CheckBox as Accept Terms and Condition and Button's Text as Submit.
Main.axml Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:text="Accept Terms and
Conditions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/checkBox1" />
<Button
android:text="Submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_Submit"
android:enabled="false" />
</LinearLayout>
|
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace CheckBoxDemo
{
[Activity(Label = "CheckBoxDemo",
MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
//Create instance of Button
Button btn_Submit;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//setting the view for the Activity
SetContentView(Resource.Layout.Main);
//Get btn_Submit Button and checkBox1 CheckBox control
from the Main.xaml Layout.
btn_Submit = FindViewById<Button>(Resource.Id.btn_Submit);
CheckBox checkBox1 =
FindViewById<CheckBox>(Resource.Id.checkBox1);
//CheckBox CheckedChange Event
checkBox1.CheckedChange += checkBox1_CheckedChange;
//btn_Submit Click Event
btn_Submit.Click += btn_Submit_Click;
}
void
checkBox1_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
//If CheckBox is Checked
if(e.IsChecked==true)
{
btn_Submit.Enabled = true;
}
else
{
btn_Submit.Enabled = false;
}
}
void btn_Submit_Click(object sender, EventArgs e)
{
Toast.MakeText(this,"Thanks for accepting Terms and Condition",ToastLength.Short).Show();
}
}
}
|
Hope you like it. Thanks.
[Download Source Code via Google Drive]