Android Boot Camp for Developers Using Java 3
Android Boot Camp for Developers Using Java, 3 E Chapter 8: Design! Using a Date. Picker on a Tablet Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 1
Objectives In this chapter, you learn to: • Create an Android project on a tablet • Understand tablet specifications • Follow design principles for the Android tablet • Add a second Android Virtual Device • Add a custom launcher and tablet theme • Understand the Calendar class • Use date, time, and clock controls • Determine the system date Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2
Objectives (continued) • Display a Date. Picker control • Launch a dialog box containing a Date. Picker control • Code an on. Date. Set. Listener method to await user interaction • Determine the date entered on a calendar control • Test an application on a tablet emulator Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3
Designing a Tablet Application • Android Apps not limited to phones • Tablets are used to – Connect to the Internet – Play games – Use social media – Check e-mail – And more… Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4
Designing a Tablet Application Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5
Designing a Tablet Application (continued) • Steps to complete the App: 1. Add an Android Virtual Device specifically designed for tablets. 2. Add the images used in this project. 3. Change theme and icon for the tablet display. 4. Create a custom XML file with a Table layout. 5. Add and initialize the Text. View controls and the Button control. 6. Initialize a Date. Picker. Dialog with the present date and listen for the user to select a date. 7. Return the selected date. 8. Display the selected reservation date in the Text. View control. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6
Designing a Tablet Application • Native applications are programs locally installed on a specific platform (phone or tablet) • An emulated application is converted in real time to run on a variety of platforms (various screens) • Android tablet apps create an optimal user experience • Different devices have different screen sizes – Common size is between 7. 3 and 10. 1 inches, 2560 x 1600 pixel resolution and 16: 10 screen ratio • i. Pad Air is 9. 7 inches, 2048 x 1536 and 4: 3 Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7
Designing a Tablet Application Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8
Design Best Practices for Tablets • • Keep screens uncluttered and use large controls Make the design simple and make the app intuitive Leave out “cool” interactions that distract the user Use flexible dimension values such as dp and sp instead of px or pt • Provide higher resolution – make your app look great on any screen size • Create a unique experience for phone and tablet designs • Use larger fonts than with a phone app. Consider printing out your user interface design to see how it looks Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9
Adding an Android Virtual Device for the Tablet • Uses Android Honeycomb 3. 2 (API 13) operating system – The minimum required SDK should be set to API 13: Android 3. 2 (Honeycomb) to cover the oldest Android tablets – Set the Target SDK to the newest SDK available • Must add Android Virtual Devices (AVD) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10
Creating a Tablet App Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11
Creating a Tablet App Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12
Creating a Tablet App Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13
Creating a Tablet App Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14
Creating the String Table Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15
Designing a Tablet Table Layout • Table. Layout is composed of Table. Row controls—one for each row in your table in activity_main. xml • The padding property can be used to offset the content of the control by a specific number of pixels. • Another Android property named typeface sets the style of the text to font families that include monospace, sans_serif, and serif Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22
Designing a Tablet Table Layout (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23
Date, Time, and Clocks • Creating a control to enter the date is crucial because requiring users to type the date in a text box can lead to multiple errors, including incorrect format or typos Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 24
Instantiating the Objects • The txt. Reservation Text. View control is referenced in multiple methods, so this instantiation must be declared as a class variable • The Text. View control, Button control, and the Button On. Click. Listener must all be instantiated Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25
Instantiating the Objects Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26
Instantiating the Objects Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27
Using the Calendar Class • The Android System date is accessed by using the Calendar class which reads the updated time zone information from the wireless provider • The get. Instance method returns the YEAR, MONTH, DAY_OF_MONTH, and the DAY_OF_YEAR Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28
Using the Calendar Class (continued) Date Format • Formats the date into a String value Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29
Using the Calendar Class (continued) Date. Picker. Dialog Input – Allows you to select a date from a Date. Picker View – Time. Picker. Dialog allows you to select a time from the Time. Picker View – The Progress. Dialog displays a progress bar below a message text box to inform you of how long a timeconsuming operation is taking – get accesses the system date or time, and set changes the current date or time Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30
Using the Calendar Class Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31
Using the Calendar Class Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32
Using the Calendar Class (continued) Selecting the Date From the Date. Picker. Dialog Adding the on. Date. Set() Method • the on. Date. Set( ) method automatically obtains the date selected by the user Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33
Using the Calendar Class Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34
Using the Calendar Class (continued) Displaying the Date Using the get. Time( ) Method • The get. Time( ) method returns the time value in the Date object Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35
Summary • When designing apps for an Android tablet, keep your users’ objectives and the size of the device in mind. • To use an Android emulator designed for tablets, you first add AVD configurations appropriate for tablets. • You can combine the Linear layout and the Table layout to create a simple, clean layout that takes advantage of a tablet’s width. The Table. Layout contains Table. Row controls—one for each row in your table in main. xml. In each Table. Row, you can insert a view control such as a Button or Text. View. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36
Summary (continued) • You can display a calendar tool called a Date. Picker control in a dialog box so users can select a date from the control. – The Time & Date category in the Palette contains many calendar controls, including Time. Picker, Date. Picker, Calendar. View, Chronometer, Analog. Clock, and Digital. Clock. • To display the current system date when the Date. Picker control opens, use the current. Year, current. Month, and current. Day class variables which hold the system year, month, and day. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 37
Summary (continued) • To create a Date. Picker. Dialog instance, you must On. Date. Set. Listener( ) method to await user interaction. • If you include a control, such as a Button, that users tap to display a calendar, use the set. On. Click. Listener method to implement the Button. On. Click. Listener. • When a dialog box containing a Date. Picker appears, users can select a date and tap a Button control. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38
Summary (continued) • When the integer id for the DATE_DIALOG_ID constant is passed to the Date. Picker dialog box in the on. Click method, a new Date. Picker Dialog is passed along with the values for year, month, and day. • After an app calls a show. Dialog(DATE_DIALOG_ID) method in reaction to the user tapping a Button control, the show. Dialog method calls the On. Create. Dialog callback method. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 39
Summary (continued) • When a dialog box containing a Date. Picker appears, users can select a date and tap a Button control. – Tapping the Button invokes an on. Date. Set. Listener in Date. Picker. Dialog, which passes integers representing the year, month, and day from the Date. Picker into on. Date. Set. – The selected date can then be displayed in a Text. View control using set. Text. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 40
- Slides: 40