Going a la Carte with Your Menu

243

Chapter 11: Handling User Input

Figure 11-1: An Edit- Text view without the gravity set. Displaying an on-screen keyboard The EditText view is very versatile and can be configured many ways. The EditText view is responsible for how the on-screen keyboard is displayed. Because some devices do not have a physical keyboard, an on-screen key- board must be present to interact with the input mechanisms. One of the properties that the EditText view provides is a way to manipulate the visual aspect of the on-screen keyboard. Why would you need to adjust the on-screen keyboard? It’s simple: Different EditText input types might need different keys. For example, if the EditText is a phone number, the on-screen keyboard should display numbers only. If the EditText value is an e-mail address, the on-screen keyboard should display common e-mail style attributes — such as an at symbol . Remember that you have various ways to configure the on-screen keyboard that, if done properly, can increase the usability of your application. 244 Part III: Creating a Feature-Rich Application You can configure the way the on-screen keyboard is configured through the inputType property on the EditText view. Far too many options exist for me to cover in this book, but you can review the various options at this URL: http:developer.android.comreferenceandroidwidget TextView.htmlattr_android:inputType. Getting Choosy with Dates and Times The application I’ve been building is a Task Reminder application — and what would a reminder application be without a way to set the date and time that the reminder should notify the user that something needs to be reviewed? Well, it wouldn’t be a Task Reminder application at all It would simply be a task list application — and that’s kind of boring if you ask me. If you’ve done any programming with dates and times in another program- ming language, you know that building a mechanism for a user to enter a date and a time can be a painstaking process all in itself. I’m happy to let you know that the Android platform has relieved all Android programmers of this issue. The Android platform provides two classes that assist you in this pro- cess: the DatePicker and TimePicker. That’s not the end of the rainbow either — these pickers also provide built-in classes that allow you to pop up a dialog box to allow the user to select a date and a time. Therefore, you can either embed the DatePicker andor TimePicker into your application’s views or you can use the Dialog classes, which can save you the process of creating a view in which to contain the DatePicker and TimePicker views. Enough jibber-jabber about what the picker widgets can do. I’m sure you’re ready to start using them, and so am I Creating picker buttons I have not added the DatePicker or TimePicker to the Task Reminder application yet, but I do so in this section. Part of the reminder_edit.xml file contains mechanisms to help show the DatePicker and TimePicker. These mechanisms are below the EditText definitions that were explained previously — I have two buttons with two 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=”stringdate” Button ➝ 4 245

Chapter 11: Handling User Input

android:id=”+idreminder_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=”stringtime” Button ➝ 12 android:id=”+idreminder_time” android:layout_height=”wrap_content” android:layout_width=”wrap_content” The code lines are explained here: ➝ 1 This is the TextView label for the Date button. This displays the value of “ReminderDate” according to the string resource. ➝ 4 This line defines a button that the user clicks to open the DatePickerDialog, as explained in the next section, “Wiring up the date picker.” ➝ 9 This is the TextView label for the Time button. This displays the value of “ReminderTime” according to the string resource. ➝ 12 This line defines a button that the user clicks to open the TimePickerDialog, as explained in the next section, “Wiring up the date picker.” Wiring up the date picker When the user clicks the Date button, he should be able to edit the date, as I show you in the following sections. Setting up the Date button click listener To implement this functionality, open the activity where your code is to be placed — for the Task Reminder application, open the ReminderEdit- Activity.java file. In the onCreate method, type the following code: registerButtonListenersAndSetDefaultText; Eclipse informs you that you need to create the method, so do that now. The easiest way to do this is by hovering over the method call squiggly, and choos- ing the “Create method registerButtonListenersAndSetDefaultText” option. In the registerButtonListenersAndSetDefaultText method, type the code shown in Listing 11-2. 246 Part III: Creating a Feature-Rich Application Listing 11-2: Implementing the Date Button Click Listener mDateButton.setOnClickListenernew View.OnClickListener { ➝ 1 Override public void onClickView v { ➝ 4 showDialogDATE_PICKER_DIALOG; ➝ 5 } }; updateDateButtonText; ➝ 8 updateTimeButtonText; ➝ 9 This code is explained as follows: ➝ 1 This line uses the mDateButton variable. As you have probably noticed, you have not defined this variable anywhere. You need to define this variable at the top of the class file. After this variable is defined, you can set the onClickListener for the button. The onClickListener is what executes when the button is clicked. The action that takes place on the button click is shown on line 5. private Button mDateButton; After this variable is created, you need to initialize it in the onCreate method right after the call to setContentView: mDateButton = Button findViewByIdR.id.reminder_date; ➝ 4 This line overrides the default click behavior of the button so that you can provide your own set of actions to perform. The View v parameter is the view that was clicked. ➝ 5 This line defines what I want to happen when the button is clicked. In this instance, I am calling a method on the base activity class — showDialog. The showDialog method I am using accepts one parameter — the ID of the dialog box that I would like to show. This ID is a value that I provide. I am providing a constant called DATE_PICKER_DIALOG. You need to define these constants at the top of the class file by typing the following code. The second constant is utilized in the section titled “Wiring up the time picker” elsewhere in this chapter. private static final int DATE_PICKER_DIALOG = 0; private static final int TIME_PICKER_DIALOG = 1; This constant provides the showDialog method with the ID that I use to show the DatePickerDialog. ➝ 8 This method is called to update the button text of the date and time buttons. This method is created in Listing 11-5. ➝ 9 This method is called to update the time button text. This method is created in Listing 11-6. 247

Chapter 11: Handling User Input