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.
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.
Step 3
Afterwards, go to Open Solution Explorer Window.
- Click Resources folder, followed by clicking Layout Folder and Open axml.
Step 4
In this step, go to Click Main.axml page, insert the code given below in Main.axml and save it.
- <?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"
- android:background="#d3d3d3">
- <TimePicker
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/TimePicker1" />
- <TextView
- android:text="Time"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txt_showTime"
- android:textColor="@android:color/black" />
- <Button
- android:text="Set Time"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:id="@+id/btnSetTime"
- android:textColor="@android:color/black"
- android:background="@android:color/holo_red_light" />
- </LinearLayout>
Proceed to click Main.axml Page Designer View, wait for few minutes and Designer View is visible.
The Designer View TableLayout is given below.
Step 5
In this step, go to Click MainActivity.cs Page, insert the code given below in MainActivity.cs and save it.
- using Android.App;
- using Android.Widget;
- using System;
- using Android.OS;
-
- namespace TimeSet
- {
- [Activity(Label = "TimeSet", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- private TextView showCurrentTime;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView (Resource.Layout.Main);
- TimePicker Tpicker = FindViewById<TimePicker>(Resource.Id.TimePicker1);
- showCurrentTime = FindViewById<TextView>(Resource.Id.txt_showTime);
- setCurrentTime();
- Button button = FindViewById<Button>(Resource.Id.btnSetTime);
- button.Click += delegate
- {
- showCurrentTime.Text = string.Format("{0}:{1}",
- Tpicker.CurrentHour, Tpicker.CurrentMinute);
- };
- }
- private void setCurrentTime()
- {
- string time = string.Format("{0}",
- DateTime.Now.ToString("HH:mm").PadLeft(2, '0'));
- showCurrentTime.Text = time;
- }
- }
- }
Step 6
- Click Build menu, followed by selecting Configuration Manager.
- Configure your Android Application to deploy & build setting, followed by clicking Close.
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.
Step 8
Output
- After a few seconds, the app will start running at your Android Simulator. You will see your app is working successfully.
Now, select Time and click Set Time. The time is set automatically in your device.
- Your Time Set Android Application is created succesfully.
Conclusion
Thus, we learned how to create a Time Set Android Application, using Visual Studio 2017.