time_picker-18478.pdf

4
Time Picker Xamarin Inc.

Transcript of time_picker-18478.pdf

Page 1: time_picker-18478.pdf

Time Picker

Xamarin Inc

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 youll 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

1 Start a new project named HelloTimePicker2 Open the ResourcesLayoutMainaxml file and insert the following

ltxml version=10 encoding=utf-8gtltLinearLayout xmlnsandroid=httpschemasandroidcomapkresandroid

androidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidorientation=verticalgtltTextView androidid=+idtimeDisplay

androidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidtext=gt

ltButton androidid=+idpickTimeandroidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidtext=Change the timegt

ltLinearLayoutgt

This is a basic LinearLayout with a TextView that will display the time and a Button that will open the TimePickerDialog

3 Open Activity1cs and insert the following class members

private TextView time_displayprivate Button pick_buttonprivate int hourprivate int minuteconst int TIME_DIALOG_ID = 0

This declares variables for the layout elements and time fields The TIME_DIALOG_ID is a static integer that uniquely identifies the dialog

4 Now insert the following code for the OnCreate() method

protected override void OnCreate (Bundle bundle) baseOnCreate (bundle)

Set our view from the Main layout resourceSetContentView (ResourceLayoutMain)

Capture our View elements time_display = FindViewByIdltTextViewgt (ResourceIdtimeDisplay

) pick_button = FindViewByIdltButtongt (ResourceIdpickTime)

Add a click listener to the button pick_buttonClick += (o e) =gt ShowDialog (TIME_DIALOG_ID)

Get the current time hour = DateTimeNowHour minute = DateTimeNowMinute

Display the current date UpdateDisplay ()

First the content is set to the Mainaxml layout and then the TextView and Button are captured with FindViewByIdltgt(int) Then the buttons Click event is attached to so that when clicked it will call ShowDialog(int) passing the unique integer ID for the time picker dialog Using ShowDialog(int) allows the Activity to manage the life-cycle of the dialog and will call the OnCreateDialog(int) callback method to request the Dialog that should be displayed (which youll define later) After the on-click listener is set we set the current hour and minute Finally the private UpdateDisplay() method is called in order to fill the TextView with the current time

5 Add the UpdateDisplay() method

Updates the time we display in the TextViewprivate voidUpdateDisplay () string time = stringFormat (01 hour minuteToString ()PadLeft (2 0)) time_displayText = time

The UpdateDisplay() method uses the member fields for the time and inserts them in the time_display TextView

6 Add a method that will be called when the user sets a new time

private void TimePickerCallback (object sender TimePickerDialogTimeSetEventArgs e) hour = eHourOfDay minute = eMinute 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 layouts TextView

7 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 Activity callback that is passed the identifier you passed to ShowDialog(int) in the Buttons on-click listener When the ID matches this initializes the TimePickerDialogwith the member variables initialized at the end of OnCreate() and the callback method created in the previous step

8 Run the application

When you press the Change the time button you should see the following

References

1 TimePicker2 Button3 TextView

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 25 Attribution License This tutorial is based on the Android Time Picker tutorial

Source URL httpxamarinalleywsguidesandroiduser_interfacetime_picker

Page 2: time_picker-18478.pdf

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 youll 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

1 Start a new project named HelloTimePicker2 Open the ResourcesLayoutMainaxml file and insert the following

ltxml version=10 encoding=utf-8gtltLinearLayout xmlnsandroid=httpschemasandroidcomapkresandroid

androidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidorientation=verticalgtltTextView androidid=+idtimeDisplay

androidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidtext=gt

ltButton androidid=+idpickTimeandroidlayout_width=wrap_contentandroidlayout_height=wrap_contentandroidtext=Change the timegt

ltLinearLayoutgt

This is a basic LinearLayout with a TextView that will display the time and a Button that will open the TimePickerDialog

3 Open Activity1cs and insert the following class members

private TextView time_displayprivate Button pick_buttonprivate int hourprivate int minuteconst int TIME_DIALOG_ID = 0

This declares variables for the layout elements and time fields The TIME_DIALOG_ID is a static integer that uniquely identifies the dialog

4 Now insert the following code for the OnCreate() method

protected override void OnCreate (Bundle bundle) baseOnCreate (bundle)

Set our view from the Main layout resourceSetContentView (ResourceLayoutMain)

Capture our View elements time_display = FindViewByIdltTextViewgt (ResourceIdtimeDisplay

) pick_button = FindViewByIdltButtongt (ResourceIdpickTime)

Add a click listener to the button pick_buttonClick += (o e) =gt ShowDialog (TIME_DIALOG_ID)

Get the current time hour = DateTimeNowHour minute = DateTimeNowMinute

Display the current date UpdateDisplay ()

First the content is set to the Mainaxml layout and then the TextView and Button are captured with FindViewByIdltgt(int) Then the buttons Click event is attached to so that when clicked it will call ShowDialog(int) passing the unique integer ID for the time picker dialog Using ShowDialog(int) allows the Activity to manage the life-cycle of the dialog and will call the OnCreateDialog(int) callback method to request the Dialog that should be displayed (which youll define later) After the on-click listener is set we set the current hour and minute Finally the private UpdateDisplay() method is called in order to fill the TextView with the current time

5 Add the UpdateDisplay() method

Updates the time we display in the TextViewprivate voidUpdateDisplay () string time = stringFormat (01 hour minuteToString ()PadLeft (2 0)) time_displayText = time

The UpdateDisplay() method uses the member fields for the time and inserts them in the time_display TextView

6 Add a method that will be called when the user sets a new time

private void TimePickerCallback (object sender TimePickerDialogTimeSetEventArgs e) hour = eHourOfDay minute = eMinute 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 layouts TextView

7 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 Activity callback that is passed the identifier you passed to ShowDialog(int) in the Buttons on-click listener When the ID matches this initializes the TimePickerDialogwith the member variables initialized at the end of OnCreate() and the callback method created in the previous step

8 Run the application

When you press the Change the time button you should see the following

References

1 TimePicker2 Button3 TextView

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 25 Attribution License This tutorial is based on the Android Time Picker tutorial

Source URL httpxamarinalleywsguidesandroiduser_interfacetime_picker

Page 3: time_picker-18478.pdf

) pick_button = FindViewByIdltButtongt (ResourceIdpickTime)

Add a click listener to the button pick_buttonClick += (o e) =gt ShowDialog (TIME_DIALOG_ID)

Get the current time hour = DateTimeNowHour minute = DateTimeNowMinute

Display the current date UpdateDisplay ()

First the content is set to the Mainaxml layout and then the TextView and Button are captured with FindViewByIdltgt(int) Then the buttons Click event is attached to so that when clicked it will call ShowDialog(int) passing the unique integer ID for the time picker dialog Using ShowDialog(int) allows the Activity to manage the life-cycle of the dialog and will call the OnCreateDialog(int) callback method to request the Dialog that should be displayed (which youll define later) After the on-click listener is set we set the current hour and minute Finally the private UpdateDisplay() method is called in order to fill the TextView with the current time

5 Add the UpdateDisplay() method

Updates the time we display in the TextViewprivate voidUpdateDisplay () string time = stringFormat (01 hour minuteToString ()PadLeft (2 0)) time_displayText = time

The UpdateDisplay() method uses the member fields for the time and inserts them in the time_display TextView

6 Add a method that will be called when the user sets a new time

private void TimePickerCallback (object sender TimePickerDialogTimeSetEventArgs e) hour = eHourOfDay minute = eMinute 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 layouts TextView

7 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 Activity callback that is passed the identifier you passed to ShowDialog(int) in the Buttons on-click listener When the ID matches this initializes the TimePickerDialogwith the member variables initialized at the end of OnCreate() and the callback method created in the previous step

8 Run the application

When you press the Change the time button you should see the following

References

1 TimePicker2 Button3 TextView

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 25 Attribution License This tutorial is based on the Android Time Picker tutorial

Source URL httpxamarinalleywsguidesandroiduser_interfacetime_picker

Page 4: time_picker-18478.pdf

) return new TimePickerDialog (this TimePickerCallback hour minute false)

return null

This is an Activity callback that is passed the identifier you passed to ShowDialog(int) in the Buttons on-click listener When the ID matches this initializes the TimePickerDialogwith the member variables initialized at the end of OnCreate() and the callback method created in the previous step

8 Run the application

When you press the Change the time button you should see the following

References

1 TimePicker2 Button3 TextView

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 25 Attribution License This tutorial is based on the Android Time Picker tutorial

Source URL httpxamarinalleywsguidesandroiduser_interfacetime_picker