+ All Categories
Home > Documents > © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly...

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly...

Date post: 17-Jan-2018
Category:
Upload: meghan-byrd
View: 214 times
Download: 0 times
Share this document with a friend
Description:
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Objectives (continued) Display a DatePicker control Launch a dialog box containing a DatePicker control Code an onDateSetListener method to await user interaction Determine the date entered on a calendar control Test an application on a tablet emulator 3 Android Boot Camp for Developers Using Java, 3rd Ed.
40
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp for Developers Using Java, 3E Chapter 8: Design! Using a DatePicker on a Tablet 1 Android Boot Camp for Developers Using Java, 3rd Ed.
Transcript
Page 1: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Android Boot Camp for Developers Using Java, 3E

Chapter 8: Design! Using a DatePicker on a Tablet

1Android Boot Camp for Developers Using Java, 3rd Ed.

Page 2: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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

2Android Boot Camp for Developers Using Java, 3rd Ed.

Page 3: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Objectives (continued)

• Display a DatePicker control• Launch a dialog box containing a DatePicker control• Code an onDateSetListener method to await user

interaction• Determine the date entered on a calendar control• Test an application on a tablet emulator

3Android Boot Camp for Developers Using Java, 3rd Ed.

Page 4: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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…

4Android Boot Camp for Developers Using Java, 3rd Ed.

Page 5: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Application (continued)

5Android Boot Camp for Developers Using Java, 3rd Ed.

Page 6: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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 the theme and icon for the tablet display.4. Create a custom XML file with a Table layout.5. Add and initialize the TextView controls and the Button control.6. Initialize a DatePickerDialog 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 TextView control.

6Android Boot Camp for Developers Using Java, 3rd Ed.

Page 7: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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• iPad Air is 9.7 inches, 2048 x 1536 and 4:3

7Android Boot Camp for Developers Using Java, 3rd Ed.

Page 8: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Application (continued)

8Android Boot Camp for Developers Using Java, 3rd Ed.

Page 9: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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

9Android Boot Camp for Developers Using Java, 3rd Ed.

Page 10: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Adding an Android Virtual Device for the Tablet

10Android Boot Camp for Developers Using Java, 3rd Ed.

• 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)

Page 11: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Creating a Tablet App

11Android Boot Camp for Developers Using Java, 3rd Ed.

Page 12: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

12Android Boot Camp for Developers Using Java, 3rd Ed.

Creating a Tablet App (continued)

Page 13: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Creating a Tablet App (continued)

13Android Boot Camp for Developers Using Java, 3rd Ed.

Page 14: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

14Android Boot Camp for Developers Using Java, 3rd Ed.

Creating a Tablet App (continued)

Page 15: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Creating the String Table (continued)

15Android Boot Camp for Developers Using Java, 3rd Ed.

Page 16: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout

16Android Boot Camp for Developers Using Java, 3rd Ed.

• TableLayout is composed of TableRow 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

Page 17: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

17Android Boot Camp for Developers Using Java, 3rd Ed.

Page 18: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

18Android Boot Camp for Developers Using Java, 3rd Ed.

Page 19: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

19Android Boot Camp for Developers Using Java, 3rd Ed.

Page 20: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

20Android Boot Camp for Developers Using Java, 3rd Ed.

Page 21: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

21Android Boot Camp for Developers Using Java, 3rd Ed.

Page 22: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

22Android Boot Camp for Developers Using Java, 3rd Ed.

Page 23: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Designing a Tablet Table Layout (continued)

23Android Boot Camp for Developers Using Java, 3rd Ed.

Page 24: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

• 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

24Android Boot Camp for Developers Using Java, 3rd Ed.

Date, Time, and Clocks

Page 25: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Instantiating the Objects

• The txtReservation TextView control is referenced in multiple methods, so this instantiation must be declared as a class variable

• The TextView control, Button control, and the Button OnClickListener must all be instantiated

25Android Boot Camp for Developers Using Java, 3rd Ed.

Page 26: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Instantiating the Objects (continued)

26Android Boot Camp for Developers Using Java, 3rd Ed.

Page 27: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Instantiating the Objects (continued)

27Android Boot Camp for Developers Using Java, 3rd Ed.

Page 28: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

• The Android System date is accessed by using the Calendar class which reads the updated time zone information from the wireless provider

• The getInstance method returns the YEAR, MONTH, DAY_OF_MONTH, and the DAY_OF_YEAR

28Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class

Page 29: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Date Format• Formats the date into a String value

29Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Page 30: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

DatePickerDialog Input– Allows you to select a date from a DatePicker View– TimePickerDialog allows you to select a time from the

TimePicker View– The ProgressDialog displays a progress bar below a

message text box to inform you of how long a time-consuming operation is taking

– get accesses the system date or time, and set changes the current date or time

30Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Page 31: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

31Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Page 32: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

32Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Page 33: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

33Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Selecting the Date From the DatePickerDialog

Adding the onDateSet() Method• the onDateSet( ) method automatically obtains the

date selected by the user

Page 34: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

34Android Boot Camp for Developers Using Java, 3rd Ed.

Using the Calendar Class (continued)

Page 35: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

35Android Boot Camp for Developers Using Java, 3rd Ed.

Displaying the Date Using the getTime( ) Method• The getTime( )

method returns the time value in the Date object

Using the Calendar Class (continued)

Page 36: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

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 TableLayout contains TableRow controls—one for each row in your table in main.xml. In each TableRow, you can insert a view control such as a Button or TextView.

36Android Boot Camp for Developers Using Java, 3rd Ed.

Page 37: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Summary (continued)

• You can display a calendar tool called a DatePicker 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 TimePicker, DatePicker, CalendarView, Chronometer, AnalogClock, and DigitalClock.

• To display the current system date when the DatePicker control opens, use the currentYear, currentMonth, and currentDay class variables which hold the system year, month, and day.

37Android Boot Camp for Developers Using Java, 3rd Ed.

Page 38: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Summary (continued)

• To create a DatePickerDialog instance, you must OnDateSetListener( ) method to await user interaction.

• If you include a control, such as a Button, that users tap to display a calendar, use the setOnClickListener method to implement the Button.OnClickListener.

• When a dialog box containing a DatePicker appears, users can select a date and tap a Button control.

38Android Boot Camp for Developers Using Java, 3rd Ed.

Page 39: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Summary (continued)

• When the integer id for the DATE_DIALOG_ID constant is passed to the DatePicker dialog box in the onClick method, a new DatePicker Dialog is passed along with the values for year, month, and day.

• After an app calls a showDialog(DATE_DIALOG_ID) method in reaction to the user tapping a Button control, the showDialog method calls the OnCreateDialog callback method.

39Android Boot Camp for Developers Using Java, 3rd Ed.

Page 40: © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Summary (continued)

• When a dialog box containing a DatePicker appears, users can select a date and tap a Button control. – Tapping the Button invokes an onDateSetListener in

DatePickerDialog, which passes integers representing the year, month, and day from the DatePicker into onDateSet.

– The selected date can then be displayed in a TextView control using setText.

40Android Boot Camp for Developers Using Java, 3rd Ed.


Recommended