Getting Choosy with Dates and Times

A Task Reminder application without a way to set the date and time is a poor Task Reminder application — it would only be a simple task list application.

If you’ve programmed dates and times in another programming language, you realize that building a mechanism for a user to enter the date and time can be a painstaking process. The Android platform comes to your rescue by providing two classes to assist you: DatePicker and TimePicker. These pickers also provide built-in classes for opening a dialog box where the user selects a date and time. Therefore, you can either embed the DatePicker or TimePicker into your application’s views or use the DialogFragment classes.

Creating picker buttons

The reminder_edit.xml file contains mechanisms to help show the DatePicker and TimePicker (under the EditText definitions described earlier). These two buttons have labels above them, as shown in Listing 11-1.

Listing 11-1: The Date and Time Buttons with Their Corresponding TextView Labels

<TextView android:layout_width=”wrap_content” →1

android:layout_height=”wrap_content”

android:text=”@string/date” />

<Button →4

android:id=”@+id/reminder_date”

android:layout_height=”wrap_content”

android:layout_width=”wrap_content”

/>

<TextView android:layout_width=”wrap_content” →9

android:layout_height=”wrap_content”

android:text=”@string/time” />

<Button →12

android:id=”@+id/reminder_time”

android:layout_height=”wrap_content”

android:layout_width=”wrap_content” ...

Get Android Application Development For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.