Xamarin.Android - How To Set Time Android Application Using Visual Studio 2017

What are Xamarin and Xamarin Forms?

  • Xamarin is a cross-platform to develop multi-platform Applications
  • Xamarin is a Shared code(C#), But Separately Design UIs Android(java), Windows(C#), IOS (Objective C & XCODE).
  • Xamarin forms UIs & shared code (C#) are same for all the projects. To develop multi-platforms Applications, run all the projects (Android, Windows, iOS) at the same time.

Prerequisites

  • Visual Studio 2017 Enterprise

The steps given below are required to be followed in order to create a Time set Android Application in Xamarin Android, using Microsoft Visual Studio 2017.

Step 1

  • Go to Visual Studio 2017, click File -> New -> Project.
  • Xamarin

Step 2

  • In this step, click C# -> Android -> Blank App(Android).
  • Enter the Application Name, followed by clicking Blank App = A project for creating a Xamarin.Android Application.
  • Xamarin

Step 3

Afterwards, go to Open Solution Explorer Window.

  • Click Resources folder, followed by clicking Layout Folder and Open axml.

    Xamarin

Step 4

In this step, go to Click Main.axml page, insert the code given below in Main.axml and save it.

Xamarin 

  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.     android:background="#d3d3d3">  
  7.   <TimePicker  
  8.       android:layout_height="wrap_content"  
  9.       android:layout_width="match_parent"  
  10.       android:id="@+id/TimePicker1" />  
  11.   <TextView  
  12.       android:text="Time"  
  13.       android:textAppearance="?android:attr/textAppearanceLarge"  
  14.       android:layout_width="match_parent"  
  15.       android:layout_height="wrap_content"  
  16.       android:id="@+id/txt_showTime"  
  17.       android:textColor="@android:color/black" />  
  18.   <Button  
  19.       android:text="Set Time"  
  20.       android:layout_width="200dp"  
  21.       android:layout_height="wrap_content"  
  22.       android:id="@+id/btnSetTime"  
  23.       android:textColor="@android:color/black"  
  24.       android:background="@android:color/holo_red_light" />  
  25. </LinearLayout>   

Proceed to click Main.axml Page Designer View, wait for few minutes and Designer View is visible.

Xamarin

The Designer View TableLayout is given below.

Xamarin

Step 5

In this step, go to Click MainActivity.cs Page, insert the code given below in MainActivity.cs and save it.

Xamarin 

  1. using Android.App;  
  2. using Android.Widget;  
  3. using System;  
  4. using Android.OS;  
  5.   
  6. namespace TimeSet  
  7. {  
  8.     [Activity(Label = "TimeSet", MainLauncher = true, Icon = "@drawable/icon")]  
  9.     public class MainActivity : Activity  
  10.     {  
  11.         private TextView showCurrentTime;  
  12.         protected override void OnCreate(Bundle bundle)  
  13.         {  
  14.             base.OnCreate(bundle);  
  15.   
  16.             // Set our view from the "main" layout resource  
  17.              SetContentView (Resource.Layout.Main);  
  18.             TimePicker Tpicker = FindViewById<TimePicker>(Resource.Id.TimePicker1);  
  19.             showCurrentTime = FindViewById<TextView>(Resource.Id.txt_showTime);  
  20.             setCurrentTime();  
  21.             Button button = FindViewById<Button>(Resource.Id.btnSetTime);  
  22.             button.Click += delegate  
  23.             {  
  24.                 showCurrentTime.Text = string.Format("{0}:{1}",  
  25. Tpicker.CurrentHour, Tpicker.CurrentMinute);  
  26.             };  
  27.         }  
  28.         private void setCurrentTime()  
  29.         {  
  30.             string time = string.Format("{0}",  
  31.                 DateTime.Now.ToString("HH:mm").PadLeft(2, '0'));  
  32.             showCurrentTime.Text = time;  
  33.         }  
  34.     }  
  35.     }   

Step 6

  • Click Build menu, followed by selecting Configuration Manager.
  • Configure your Android Application to deploy & build setting, followed by clicking Close.

    Xamarin

Step 7

In this step, select build & deploy option, followed by clicking Start your Application.

Now, go to Run option, choose Debug, select any one Simulator and run it.

Xamarin

Step 8

Output

  • After a few seconds, the app will start running at your Android Simulator. You will see your app is working successfully.

    Xamarin

Now, select Time and click Set Time. The time is set automatically in your device.

Xamarin

  • Your Time Set Android Application is created succesfully.

Conclusion

Thus, we learned how to create a Time Set Android Application, using Visual Studio 2017.

Up Next
    Ebook Download
    View all
    Learn
    View all