Xamarin.Android | ComponentOne
Controls / Input / Toggle Button / Quick Start: Change State and Customize the Control
In This Topic
    Quick Start: Change State and Customize the Control
    In This Topic

    This section describes adding the C1ToggleButton control to your portable or shared application and changing color of the control on the basis of change in state of the control.

    Complete the following steps to change color of the control on changing its state.

    Step 1: Initialize C1ToggleButton in code

    1. Open the .axml file in your layout folder from the Solution Explorer and replace the code with following code to add C1ToggleButton control to your layout.
      XML
      Copy Code
      <?xml version="1.0" encoding="utf-8"?>
      <C1.Android.Core.C1ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tb"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

      Alternatively, you can drag C1ToggleButton control from the Toolbox within the custom control tab onto your layout surface in designer mode. Then, inside your activity, add the following code to initialize your layout and add data for C1ToggleButton control.

      C#
      Copy Code
      public class MainActivity : Activity
      {
           protected override void OnCreate(Bundle savedInstanceState)
           {
                base.OnCreate(savedInstanceState);
      
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
      
                // Get our button from the layout resource,
                // and attach an event to it
      
      
                C1ToggleButton tb = (C1ToggleButton)this.FindViewById(Resource.Id.tb);
      
                tb.Click += (o, e) =>
                {
                     // Perform action on clicks
                     if (tb.IsChecked == true)
                     {
                        tb.Color=Color.Green;
                     }
                     else if (tb.IsChecked == false)
                     {
                        tb.Color = Color.Red;
                     }
                };
           }
      }

    Back to Top

    Step 2: Run the Project

    Press F5 to run your application.

    Back to Top