Getting Dates and Times from Users Android

The Android SDK provides a couple controls for getting date and time input from the user.The first is the DatePicker control. It can be used to get a month, day, and year from the user.

Date and time controls.

Date and time controls.

The basic XML layout resource definition for a DatePicker follows:

As you can see from this example, there aren’t any attributes specific to the DatePicker control.As with many of the other controls, your code can register to receive a method call when the date changes.You do this by implementing the onDateChanged() method. However, this isn’t done the usual way.

The preceding code sets the DatePicker.OnDateChangedListener by a call to the DatePicker.init() method.A DatePicker control is initialized with the current date.A Text View is set with the date value that the user entered into the DatePicker control. The value of 1900 is subtracted from the year parameter to make it compatible with the java.util.Date class.

A TimePicker control is similar to the DatePicker control. It also doesn’t have any unique attributes. However, to register for a method call when the values change, you call the more traditional method of

As in the previous example, this code also sets a TextView to a string displaying the time value that the user entered. When you use the DatePicker control and the TimePicker control together, the user can set a full date and time.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics