Time Picker
- PDF for offline use
Let us know how you feel about this
Translation Quality
0/250
last updated: 2017-08
To provide a widget for selecting a time, use the
TimePicker
widget, which allows the user to select the hour and minute in a
familiar interface.
In this tutorial, you'll create a
TimePickerDialog,
which presents the time picker in a floating dialog box at the press of
a button. When the time is set by the user, a
TextView
will update with the new date.
Start a new project named HelloTimePicker.
Open the Resources/Layout/Main.axml file and insert the following:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/timeDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/pickTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change the time"/> </LinearLayout>This is a basic
LinearLayoutwith aTextViewthat will display the time and aButtonthat will open theTimePickerDialog.Open MainActivity.cs and insert the following class members:
private TextView time_display; private Button pick_button; private int hour; private int minute; const int TIME_DIALOG_ID = 0;This declares variables for the layout elements and time fields. The
TIME_DIALOG_IDis a static integer that uniquely identifies the dialog.Now insert the following code for the
OnCreate()method:protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "Main" layout resource SetContentView (Resource.Layout.Main); // Capture our View elements time_display = FindViewById<TextView> (Resource.Id.timeDisplay); pick_button = FindViewById<Button> (Resource.Id.pickTime); // Add a click listener to the button pick_button.Click += (o, e) => ShowDialog (TIME_DIALOG_ID); // Get the current time hour = DateTime.Now.Hour; minute = DateTime.Now.Minute; // Display the current date UpdateDisplay (); }First, the content is set to the Main.axml layout and then the
TextViewandButtonare captured withFindViewById. Then the button's Click event is attached to, so that when clicked, it will callShowDialog(int), passing the unique integer ID for the time picker dialog. UsingShowDialog(int)allows theActivityto manage the life-cycle of the dialog and will call theOnCreateDialog(int)callback method to request theDialogthat should be displayed (which you'll define later). After the on-click listener is set, we set the current hour and minute. Finally, the privateUpdateDisplay()method is called in order to fill theTextViewwith the current time.Add the
UpdateDisplay()method:// Updates the time we display in the TextView private void UpdateDisplay () { string time = string.Format ("{0}:{1}", hour, minute.ToString ().PadLeft (2, '0')); time_display.Text = time; }The
UpdateDisplay()method uses the member fields for the time and inserts them in thetime_displayTextView.Add a method that will be called when the user sets a new time:
private void TimePickerCallback (object sender, TimePickerDialog.TimeSetEventArgs e) { hour = e.HourOfDay; minute = e.Minute; UpdateDisplay (); }When the user is done setting the time (clicks the Set button), this method is called and it updates the member fields with the new time and updates the layout's
TextView.Add the
OnCreateDialog(int)callback method:protected override Dialog OnCreateDialog (int id) { if (id == TIME_DIALOG_ID) return new TimePickerDialog (this, TimePickerCallback, hour, minute, false); return null; }This is an
Activitycallback that is passed the identifier you passed toShowDialog(int)in theButton's on-click listener. When the ID matches, this initializes theTimePickerDialogwith the member variables initialized at the end ofOnCreate()and the callback method created in the previous step.Run the application.
When you press the Change the time button, you should see the following:
References
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Let us know how you feel about this
Translation Quality
0/250
Xamarin Workbook
If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.


