Creating A Switch In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform for developing cross-platform and multi-platform apps (Ex. Windows phone, Android, iOS). In Xamarin platform, code sharing concept is used. The Xamarin Studio is available in Visual Studio also.

The Switch is used to change a setting between two states (ON and OFF).

Prerequisites

  • Visual Studio 2015 update 3

The following steps are needed to be followed in order to create a Switch in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> New--> Project, or click (Ctrl+Shift+N) to open the list of different project types.

Xamarin

Step 2

Select Installed -->Templates -->Visual C# --> Android -->choose Blank App (Android).

Next, give your Android app a name (Ex:sample) and give path of your project. Click OK.

Xamarin

Step 3

Now, go to the Solution Explorer, select Resource-->Layout--> double click to open Main.axml page. You can either select designer or source to design your application's homepage.

Xamarin

Step 4

In the main page designer, delete the default "Hello World" button. Go to the source panel and remove the button coding.

Xamarin

Now, delete the C# button action code from MainActivity.cs page.

Step 5

Next, go to the toolbox window and scroll down. Drag and drop the Switch on the page, wherever you want.

Xamarin

Step 6

Next, go to the properties window and edit the Switch Text, Id and checked properties values.

(Ex: android:text="Are you love Xamarin...?" android:id="@+id/monitored_switch" android:checked="true").

Xamarin

Step 7

In this step, edit the Switch textOn and textOff value (Ex: android:textOn="On" android:textOff="Off" ).

Xamarin

Step 8

In this step, you will see the Main.axml page source panel. Note the id value.

Main.axml

  1. <Switch  
  2. android:text="Are you love Xamarin...?"  
  3. android:layout_width="match_parent"  
  4. android:layout_height="wrap_content"  
  5. android:checked="true"  
  6. android:textOn="YES"  
  7. android:textOff="NO"  
  8. android:id="@+id/monitored_switch" />  
Xamarin

Step 9

Next, go to MainActivity.cs page and write the following code between the OnCreate() Method.

MainActivity.cs

  1. protected override void OnCreate(Bundle bundle) 
  2. {  
  3.     base.OnCreate(bundle);  
  4.     // Set our view from the "main" layout resource  
  5.     SetContentView(Resource.Layout.Main);  
  6.     Switch s = FindViewById < Switch > (Resource.Id.monitored_switch);  
  7.     s.CheckedChange += delegate(object sender, CompoundButton.CheckedChangeEventArgs e)
  8.  {  
  9.         var toast = Toast.MakeText(this, "I Love Xamarin !" +  
  10.             (e.IsChecked ? "Yes" : " No"), ToastLength.Short);  
  11.         toast.Show();  
  12.     }
  13. }  
Xamarin

Step 10

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio.

The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone. If Enabled, the Switch will show "I Love Xamarin ! Yes".



If disabled, the Switch shows, " I Love Xamarin ! No".

Xamarin

Summary

So, this was the process of creating a Switch in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all