What are Xamarin and Xamarin Forms?

Prerequisites

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

Step 2

Step 3

Afterwards, go to Open Solution Explorer Window.

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

Step 6

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

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

Xamarin

Conclusion

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