Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Monday, 29 December 2014

CheckBox Widget in Xamarin

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.
Create Android Application in Xamarin
2) Drop a CheckBox (with id="@+id/checkBox1") and Button (with id="@+id/btn_Submit") control onto the Layout, Main.axml.
CheckBox Widget in Xamarin
Set the Text of CheckBox as Accept Terms and Condition and Button's Text as Submit.
CheckBox and Button in Xamarin
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>
In Activity1.cs, We have created CheckBox CheckedChange Event. The following is Activity1.cs Code:
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();
        }
    }
}
Final Preview:
CheckBox and Button in Xamarin / Mono for Android
Hope you like it. Thanks.
[Download Source Code via Google Drive]

2 comments:

  1. how to save multiple checkbox value in one record of sqlite database in xamarin using c#??

    ReplyDelete
  2. how to save multiple checkbox value in one record of sqlite database in xamarin using c#??

    ReplyDelete

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook

Blog Archive