Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 1
1
Android Bootcamp
for Developers
Using Java, 4e
Chapter 1: Voilà! Meet the
Android
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 2
Icebreaker
• The class will be broken up into pairs of students.
• Each student will create three questions to ask their partner:
− One true/false question
− One question that can be answered with a number
− One question that requires a short answer
• Partners will take turns asking the questions they’ve prepared.
• Each student will then introduce their partner to the class using the information
they’ve learned.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 3
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 01.01 Describe the market for Android applications
• 01.02 Identify the role of the Android platform in the mobile market
• 01.03 Describe the features of the Android phone
• 01.04 Identify the languages used in Android development
• 01.05 Describe the role of Google Play in the marketplace
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 4
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 01.06 Understand the connection between Windows and Android
• 01.07 Create an Android project using Android Studio
• 01.08 Design the user interface layout within the virtual device
• 01.09 Execute an Android application on an emulator
• 01.10 Open a saved Android project in Android Studio
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 5
1.1 Understanding the Market for Android
Applications
• Two primary mobile operating systems
− iOS for iPhone
− Google Android
• Recently, the Google Android phone has become the sales leader
• Apps: mobile applications created for smartphones
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 6
1.2 The Role of the Android in the Mobile
Market
• Open-Source Platform
− Android is an open-source operating system—no company or individual defines
the features or direction of the development
− Open Handset Alliance: an open-source business alliance of 80 firms that develop
standards for mobile devices
• Android Open-Source Devices
− Android smartphones, tablets, and smartwatches are sold by many companies
− As you develop an app, you test it using an emulator, which duplicates how the
app looks and feels on a particular device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 7
1.3 Features of the Android Phone (1 of 3)
Feature Description
3D graphics The interface can support 3D graphics for a 3D interactive game experience or 3D
image rendering.
Split screen A split-screen, multiwindow feature displays more than one Android application at
the same time. Watch a movie on the left side and hold a video call with your
friends who are also watching the movie.
Dark mode The Dark mode feature helps to save the phone’s battery life. Blank white space
consumes more power and drains the battery.
Facial recognition Android provides this high-level feature for automatically identifying or verifying a
person’s face from a digital image or a video frame.
Front- and rear-facing
camera
Android phones can use either a front- or rear-facing camera, allowing developers
to create applications involving video calling.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 8
1.3 Features of the Android Phone (2 of 3)
Feature Description
Multiple language
voice-to-text support
Android supports multiple human languages. Android provides a feature of
converting text to speech in different languages. Text messages can be read aloud
to help people who are visually impaired.
On-screen keyboard The on-screen keyboard offers suggestions for spelling corrections as well as
options for completing words you start typing. The on-screen keyboard also
supports voice input.
Power management Android identifies programs running in the background that are using memory and
processor resources. You can close those apps to free up the phone’s processor
memory, extending the battery power.
Gaming For optimized gaming, Android supports the use of a gyroscope, gravity and
barometric sensors, linear acceleration, and rotation vectors, which provide game
developers with highly sensitive and responsive controls.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 9
1.3 Features of the Android Phone (3 of 3)
Feature Description
Voice-based
recognition
Android recognizes voice actions for calling, texting, and navigating with the phone.
Wi-Fi Internet tethering Android supports tethering, which allows a phone to be used as a wireless or wired
hotspot that other devices can use to connect to the Internet.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 10
1.4 Languages Used in Android Development
(1 of 4)
• Java is an object-oriented programming language patterned after the C++
language
• Java and Kotlin can be used to code Android apps
• Android Studio
− An integrated development environment (IDE) for building and integrating
application development tools and open-source projects
− Includes the Android Software Development Kit (SDK)
− XML is used to assist in the layout of the Android emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 11
1.4 Languages Used in Android Development
(2 of 4)
Figure 1-4 Android Studio
running Java code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 12
1.4 Languages Used in Android Development
(3 of 4)
• Android Emulator
− Design, develop, prototype, and test Android apps without using a physical
device
− Mimics almost every feature of a real Android handset, except placing phone
calls
• Versions of Android
− First release was 1.0 (September 2008)
− Each subsequent release adds features and fixes known bugs
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 13
1.4 Languages Used in Android Development
(4 of 4)
• Versions of Android (continued)
− Android has adopted a naming system based on treats/desserts
− When the revision level reached 10.0, Android started emphasizing revision
number over names
− Most recent versions were Android 12.0 (Snow Cone, October 2021) and
Android 13.0 (Tiramisu, October 2022)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 14
Activity 1.1: Knowledge Check
(1 of 2)
1. What is the name of the organization that sets the standards for Android
mobile devices?
2. Name at least four types of devices that run the Android OS.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 15
Activity 1.1: Knowledge Check
(2 of 2)
3. Should a developer target only the most recent version of the Android OS
when creating an app?
4. What is the name of the current version of the Android OS?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 16
Activity 1.1: Knowledge Check Answers
(1 of 2)
1. What is the name of the organization that sets the standards for Android
mobile devices?
Answer: Open Handset Alliance
2. Name at least four types of devices that run the Android OS.
Answer: Smartphones, foldable phones, tablets, watches, MP4 players,
automobiles, and Internet TVs
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 17
Activity 1.1: Knowledge Check Answers
(2 of 2)
3. Should a developer target only the most recent version of the Android OS
when creating an app?
Answer: No. To support as many Android devices as possible, you should set
the minimum development version to the lowest available that allows your app
to provide its core feature set.
4. What is the name of the current version of the Android OS?
Answer: Android 13.0 (internal name: Tiramisu)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 18
1.5 Google Play in the Marketplace (1 of 2)
• The Android platform consists of the Android OS, application development tools,
and a marketplace
• Apps are compiled into package files with an .apk extension
• Google Play (http://play.google.com) sells and deploys all apps
• Programs must meet minimum standards, including:
− Reliability
− Efficiency
− Performance
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 19
1.5 Google Play in the Marketplace (2 of 2)
• Developers must sign an agreement and pay a one-time registration fee
(currently $25) to publish apps on Google Play
• Apps offered for free are free to publish
• For paid apps, standard split is 85% of sales for the developer and 15% for
wireless carriers for the first $1 million of annual earnings selling digital goods or
services
• Also sold through the Amazon Appstore
− Pays developers 80% of the sale price for sold apps and grants 10% of their
Appstore revenue as an Amazon Web Services credit
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 20
1.6 The Connection between Windows and
Android
• Beginning with Windows 11, you can download Android apps to your PC from
the Microsoft Store
• Android apps are also available for Windows-based tablets and other touch
devices
• Phone Link App
− The Microsoft Phone Link app allows you to connect an Android phone to a
Windows 11 PC
− Enables text messaging, notifications, and transfer of photos from the phone
on the PC
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 21
1.7 Creating an Android Project Using
Android Studio (1 of 8)
• Install Android Studio from developer.android.com/studio
• Opening Android Studio to Create a New Project
− Name: the human-readable title for your app
− Package name: the Java package namespace for the source code
− Save location on your hard drive or USB drive
− Minimum SDK: the minimum API level required by your app
− Language (Java or Kotlin)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 22
1.7 Creating an Android Project Using
Android Studio (2 of 8)
• Creating the Hello Android World Project
− Android Studio facilitations app creation for various form factors
− Two most common projects: Empty Activity and Basic Project
− To create a new Android project, you open Studio, click the New Project
button, and make selections and entries for the project specs
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 23
1.7 Creating an Android Project Using
Android Studio (3 of 8)
Figure 1-8 Welcome to
Android Studio window
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 24
1.7 Creating an Android Project Using
Android Studio (4 of 8)
Figure 1-9 New Project
window with Empty Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 25
1.7 Creating an Android Project Using
Android Studio (5 of 8)
Figure 1-10 Creating Empty
Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 26
1.7 Creating an Android Project Using
Android Studio (6 of 8)
Figure 1-11 Android Studio
project view
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 27
1.7 Creating an Android Project Using
Android Studio (7 of 8)
• Building the User Interface
− The user interface must be intuitive
− The user interface must not distract from functionality
− Java code or XML layout files are needed
− The XML method is preferred because it allows you to design a user
interface without writing large amounts of code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 28
1.7 Creating an Android Project Using
Android Studio (8 of 8)
• The Android Project View
− The Android project view contains the key source application folders for a
project: manifests, java, and res
− The manifests folder includes the AndroidManifest.xml file, which
contains all the information about the application that Android needs to run
− The java folder contains the Java code source files
− The res folder contains resources the app may need: images, music, video,
and the user interface named activity_main.xml
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 29
1.8 The User Interface Layout within the
Virtual Device (1 of 12)
• The Android SDK includes layout files called activity_main.xml in the res/layout
folder of the app
• The Layout Editor contains the Palette, Component Tree, toolbar, Design
editor, and Attributes pane
• Layout: a container that holds as many widgets as needed
• Widget: a single element (a.k.a. object), such as a Button from the Palette
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 30
1.8 The User Interface Layout within the
Virtual Device (2 of 12)
Figure 1-12 Coding window in
Android Studio
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 31
1.8 The User Interface Layout within the
Virtual Device (3 of 12)
• The Attributes pane contains the attributes and settings of the currently active
app project or object
• Attribute: a characteristic of an object that describes what that object can do
• Android Studio displays an emulator configuration called the Android Virtual
Device (AVD) for design and layout purposes
• To change the virtual device, you open the activity_main.xml file to open the
Layout Editor, then click the Device for Preview (D) button and select the
desired device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 32
1.8 The User Interface Layout within the
Virtual Device (4 of 12)
Figure 1-13 Layout Editor for
emulator layout design
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 33
1.8 The User Interface Layout within the
Virtual Device (5 of 12)
Figure 1-14 Device for
Preview button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 34
1.8 The User Interface Layout within the
Virtual Device (6 of 12)
Figure 1-15 Generic phone
selected for Device for
Preview
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 35
1.8 The User Interface Layout within the
Virtual Device (7 of 12)
• When you select the default TextView component, its text is displayed in the
Component Tree
− The Component Tree displays widgets in the order they were placed on the
emulator
• Within the Common Attributes section of the Attributes pane, you can see the
text attribute of the component, which is the text it contains
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 36
1.8 The User Interface Layout within the
Virtual Device (8 of 12)
Figure 1-16 Design editor
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 37
1.8 The User Interface Layout within the
Virtual Device (9 of 12)
Figure 1-17 Attributes pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 38
1.8 The User Interface Layout within the
Virtual Device (10 of 12)
• The TextView Component
− Text can be updated by:
 Changing the hard-coded value for the text attribute in the Attributes
pane
 Modifying resource files
 Changing the application XML source code
− It is good programming practice to add an id attribute to each component
(object) for use in Java code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 39
1.8 The User Interface Layout within the
Virtual Device (11 of 12)
Figure 1-18 Emulator with
TextView control and text
attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 40
1.8 The User Interface Layout within the
Virtual Device (12 of 12)
Figure 1-19 The id attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 41
1.9 Executing an Android Application on an
Emulator (1 of 2)
• The Android emulator (virtual device) runs slowly
− Even when idling, it consumes significant CPU time
• To run and automatically save your app, click the Run button
• Click the Terminate button to stop the emulator from running
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 42
1.9 Executing an Android Application on an
Emulator (2 of 2)
Figure 1-20 Hello Android
World app displayed in
emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 43
Activity 1.2: Knowledge Check
1. In Android Studio, objects such as buttons and text boxes are called _____.
2. Java code source files are stored in the _____ folder.
3. Testing the app is done in a(n) _____.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 44
Activity 1.2: Knowledge Check Answers
1. In Android Studio, objects such as buttons and text boxes are called _____.
Answer: widgets
2. Java code source files are stored in the _____ folder.
Answer: java
3. Testing the app is done in a(n) _____.
Answer: emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 45
1.10 Opening a Saved Android Project in
Android Studio
• To load a saved project, open Studio and then choose the project to open
− Click on it in the Recent Projects column or
− Click Open an existing Android Project in the Quick Start column, navigate to
it, and click the OK button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 46
Self-Assessment
1. After reading about the market and requirements for software development for
Android devices, what skills would you like to gain or improve before you
begin developing Android apps for publication? What aspects of the market for
these apps would you like to learn more about?
2. What is your initial impression of the Android Studio and Android SDK tools
available to you as an Android developer?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 47
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 48
48
Android Bootcamp
for Developers
Using Java, 4e
Chapter 2: Simplify! The Android
User Interface
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 49
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 02.01 Develop a user interface using the TextView, ImageView, and Button
components
• 02.02 Add text using the String table in the Translations Editor
• 02.03 Construct the user interface using a ConstraintLayout
• 02.04 Plan out program design
• 02.05 Create multiple Android activities
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 50
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 02.06 View activities in the Android Manifest file
• 02.07 Write code using the onCreate( ) method
• 02.08 Create a Button event handler
• 02.09 Code a Button event handler
• 02.10 Correct errors in code
• 02.11 Run the completed app in the emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 51
2.1 Designing an Android App (1 of 5)
Figure 2-1 Easy Recipes
Android app (left)
Figure 2-2 Second screen with
recipe (right)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 52
2.1 Designing an Android App (2 of 5)
• Designing apps is like constructing a building
• The Big Picture: To create the Easy Recipes app, you follow a set of steps that
you repeat every time you create an Android application
− Create the user interface (XML layout) for every screen
− Create a Java class (Activity) for every screen
− Code each Java class with appropriate objects and actions
− Test the application in the Android phone emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 53
2.1 Designing an Android App (3 of 5)
• Using the Android User Interface
− Interface: the space where interactions occur between people and the app
− Android apps run on various form factors
− The layout is stored as XML code
 Special Android-formatted XML code is extremely compact
 XML is a set of instructions for placing components on the screen
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 54
2.1 Designing an Android App (4 of 5)
Figure 2-4 Creating an empty
activity for the Easy Recipes
app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 55
2.1 Designing an Android App (5 of 5)
Figure 2-5 Android
Studio open and
ready for you to
design the Easy
Recipes app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 56
2.2 Using the String Table in the Translations
Editor (1 of 5)
• The Translations Editor in Android Studio can display the original text in the
language in which it was programmed, or the language of the current locale plus
culturally specific settings
• The String Table
− Android provides the strings.xml file or String table inside the Resources
folder
− strings.xml: a default file that is part of every Android application and
contains commonly used strings for an application
− string: a series of alphanumeric characters that can include spaces
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 57
2.2 Using the String Table in the Translations
Editor (2 of 5)
• The String Table (continued)
− Localization: the use of the String table to change text based on the user’s
preferred language
− Every string is composed of a key (a.k.a. id attribute), which is the name of
a component, and a default value, which is the text associated with the
component
− To create an application that is accessible to those with visual disabilities,
add descriptions to the user interface components that can be read out
loud to your user by a speech-based accessibility service such as a screen
reader
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 58
2.2 Using the String Table in the Translations
Editor (3 of 5)
Figure 2-7 The strings.xml tab
displaying XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 59
2.2 Using the String Table in the Translations
Editor (4 of 5)
Figure 2-10 Adding another
string to the String table
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 60
2.2 Using the String Table in the Translations
Editor (5 of 5)
Figure 2-11 Completed String
table on the Translations
Editor tab
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 61
2.3 Constructing the User Interface with the
ConstraintLayout (1 of 18)
• The Android user interface includes a layout resource designer called the
ConstraintLayout in the Component Tree
− Organizes how components appear on the app’s screens
− Provides more flexibility in positioning components than other layouts
− Constraints: the building blocks in an Android layout that attach or align a
component to the screen, either by attaching it vertically and horizontally to
the edges of the screen or aligning it with another component (or parent) on
the screen
− Constraints are used to create a Responsive Design UI
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 62
2.3 Constructing the User Interface with the
ConstraintLayout (2 of 18)
• Constraint Handles
− Four circular dots on the edges of a selected component
− A Constraint component must have at least two constraints—a vertical and a
horizontal one
− If you drag all four Constraint handles to the four edges of the emulator
screen, you can then drag the component anywhere on the screen and it will
stay in that location when you run the app
− In the ConstraintLayout, a nonconstrained component will float to the top of
the screen when the app is run
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 63
2.3 Constructing the User Interface with the
ConstraintLayout (3 of 18)
• Constraint Handles (continued)
− A white Constraint handle means
that the component is not aligned
− A blue Constraint handle means
that the component is aligned with
the screen or another component
− Corner boxes resize the
component
Figure 2-12 The four circles
are Constraint handles
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 64
2.3 Constructing the User Interface with the
ConstraintLayout (4 of 18)
• XML Code
− After you move the Constraint handles in the Design view, Android Studio
updates your layout resources as XML code in the res/layout resource
directory
− Other common layouts used in Android Studio include:
 LinearLayout
 RelativeLayout
 TableLayout
 GridView
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 65
2.3 Constructing the User Interface with the
ConstraintLayout (5 of 18)
Figure 2-13 ConstraintLayout
XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 66
2.3 Constructing the User Interface with the
ConstraintLayout (6 of 18)
• The Easy Recipes Opening User Interface: To change a component's attributes,
select it and use the Attributes pane
• Android Text Attributes
− The text attribute uses text from a string resource to display within the
component
− The textSize attribute can use various units of measurement
 The preferred unit is often sp (scaled-independent pixels)
 Other units: inches (in), millimeters (mm), pixels (px), density-
independent pixels (dp or dip)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 67
2.3 Constructing the User Interface with the
ConstraintLayout (7 of 18)
Figure 2-16 The text attribute
with @string used to display
the String table
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 68
2.3 Constructing the User Interface with the
ConstraintLayout (8 of 18)
Figure 2-17 TextView text
displayed in emulator after
@string selected
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 69
2.3 Constructing the User Interface with the
ConstraintLayout (9 of 18)
Figure 2-18 Updating the
textSize of the TextView
component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 70
2.3 Constructing the User Interface with the
ConstraintLayout (10 of 18)
• Adding a File to the Resources Folder
− Graphics used in ImageView components must first be placed in the
res/drawable folder
− Android supports .png (preferred), .jpg (acceptable), and .gif (discouraged)
graphic formats
− Android categorizes device screens based on size and density
− Android creates a Drawable resource for images when you save them in the
res/drawable folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 71
2.3 Constructing the User Interface with the
ConstraintLayout (11 of 18)
Figure 2-19 The sorbet.png
file copied into the drawable
folder within the res folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 72
2.3 Constructing the User Interface with the
ConstraintLayout (12 of 18)
• The ImageView Component
− An ImageView component can display an icon, a company logo, or a
graphic on the Android screen
− Reminder: The image to be used should be placed in a drawable resources
folder before the ImageView component is placed in the emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 73
2.3 Constructing the User Interface with the
ConstraintLayout (13 of 18)
Figure 2-23 ImageView
component with image
displayed in the emulator and
the id entered
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 74
2.3 Constructing the User Interface with the
ConstraintLayout (14 of 18)
Figure 2-24 The
contentDescription set for
accessibility
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 75
2.3 Constructing the User Interface with the
ConstraintLayout (15 of 18)
• Adding a Button Component
− Button components are commonly used in GUIs
− The Android SDK includes four types of button components: Button,
FloatingActionButton, ToggleButton, and ImageButton
− backgrountTint: an attribute that can be added to a component to set a
background color
− textColor: an attribute that sets the color of the text in a component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 76
2.3 Constructing the User Interface with the
ConstraintLayout (16 of 18)
Figure 2-26 Button
component placed on
emulator and the id named
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 77
2.3 Constructing the User Interface with the
ConstraintLayout (17 of 18)
Figure 2-27 Button displaying
larger text
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 78
2.3 Constructing the User Interface with the
ConstraintLayout (18 of 18)
Figure 2-28 Button with
colors displayed for
background tint color
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 79
Activity 2.1: Knowledge Check (1 of 2)
1. List the major steps you follow to create an Android application.
2. Android loads text resources from the project's _____.
3. Which Android layout provides flexibility in positioning components and
facilitates making your layout a Responsive Design UI?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 80
Activity 2.1: Knowledge Check (2 of 2)
4. Which unit of measurement is often preferred for the textSize attribute? Why?
5. To display an illustration or photo in an Android app you use a(n) _____.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 81
Activity 2.1: Knowledge Check Answers
(1 of 2)
1. List the major steps you follow to create an Android application.
Answer: (1) Create the user interface (XML layout) for each screen.
(2) Create a Java class (Activity) for every screen.
(3) Code each Java class with the appropriate objects and actions.
(4) Test the application in the emulator.
2. Android loads text resources from the project's _____.
Answer: string table or strings.xml file
3. Which Android layout provides flexibility in positioning components and
facilitates making your layout a Responsive Design UI?
Answer: ConstraintLayout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 82
Activity 2.1: Knowledge Check Answers
(2 of 2)
4. Which unit of measurement is often preferred for the textSize attribute? Why?
Answer: sp (scaled-independent pixels), because it will scale the font to meet
the user's size preference
5. To display an illustration or photo in an Android app you use a(n) _____.
Answer: ImageView component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 83
2.4 Planning Out Program Design
• Phases in the program development life cycle:
− Gather and analyze the program requirements
− Design the user interface
− Design the program processing objects
− Code the program
− Test the program
• Creating Activities
− Activity: a screen, constructed from XML files and a Java class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 84
2.5 Creating Multiple Android Activities
(1 of 2)
• A class describes a group of objects and serves as a blueprint, or template, for
creating those objects
• An object is a specific, concrete instance of a class
• When you create an object, you instantiate it, meaning you define one
particular variation of the object within a class
• Each screen represents one Activity, and each Activity must have a matching
Java class file within the java folder
• The convention is to use Pascal case for Java class names
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 85
2.5 Creating Multiple Android Activities
(2 of 2)
Figure 2-34 New Recipe class
in the Easy Recipes project
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 86
2.6 Viewing Activities in the Android Manifest
File (1 of 3)
• The Android Manifest file (AndroidManifest.xml) contains:
− The name of the Java application
− A listing of each activity
− Permissions needed to access other Android functions (such as the Internet,
services, and broadcast receivers)
− The minimum level of the Android API
• Android Studio automatically adds each activity you create to the Android
Manifest file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 87
2.6 Viewing Activities in the Android Manifest
File (2 of 3)
Figure 2-35
AndroidManifest.xml
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 88
2.6 Viewing Activities in the Android Manifest
File (3 of 3)
• Coding the Android Activity
− The entry point of the Activity class is the onCreate() event handler method
 Where you initialize the Activity by placing it on the top of Android's stack
of Activities
− Method: a set of Java statements that can be included inside a Java class; a
piece of code that performs some type of action
− Method body: a collection of statements that defines what the method does
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 89
2.7 Writing Code Using the onCreate()
Method (1 of 2)
• Syntax for the onCreate() method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
• This method has a void return type, which means it does not return anything
• The Java code necessary to display the content of a specific screen is called
SetContentView, which has the following syntax:
setContentView(R.layout.activity_main);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 90
2.7 Writing Code Using the onCreate()
Method (2 of 2)
Figure 2-36 MainActivity.java
code for onCreate( ) method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 91
2.8 Creating a Button Event Handler
(1 of 3)
• Event handler: a part of a program coded to respond to a specific event
• Tapping a button is called a click event in Java code
• Java code for an event handler must contain the following sections:
− A class attribute to hold a reference to the Button object:
Button = (Button)findViewById(R.id.btRecipe);
 Includes the findViewById() method to find a layout XML file
− An OnClickListener() method to await the button click action's onClick()
method to respond to the click event
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 92
2.8 Creating a Button Event Handler
(2 of 3)
• When you import the Button type as an Android widget, the button package
becomes available throughout the app
− An import statement makes more Java functions available to your app
• After the Button attribute is referenced in MainActivity.java, an OnClickListener()
method is necessary to detect when the user taps an on-screen button
• When an OnClickListener is placed in the code window, Java creates an onClick
auto-generated stub—a piece of code that serves as a placeholder to declare
itself
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 93
2.8 Creating a Button Event Handler
(3 of 3)
• Syntax for an OnClickListener() method that listens for a Button:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
• The startActivity() method creates an intent to start another Activity
− Two parameters: context and name of Activity to open
− Syntax: startActivity(new Intent(MainActivity.this,
Recipe.class));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 94
2.9 Coding a Button Event Handler
(1 of 3)
Figure 2-38 Button
instantiated with Button
library added
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 95
2.9 Coding a Button Event Handler
(2 of 3)
Figure 2-40 The
setOnClickListener added
with auto-complete feature
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 96
2.9 Coding a Button Event Handler
(3 of 3)
Figure 2-41 Complete code
for MainActivity.java—
startActivity will launch the
second screen’s Recipe.class
when the user clicks the
button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 97
Activity 2.2: Knowledge Check (1 of 2)
1. Name the phases of the development life cycle in order.
2. The file included in each Android application that provides essential
information such as the minimum API level to the Android device is called
_____.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 98
Activity 2.2: Knowledge Check (2 of 2)
3. Which Java method initializes an Android Activity?
4. A part of a program that responds to a user interaction such as tapping an on-
screen button is called a(n) _____.
5. Which Java method is necessary to detect a user interaction with the app?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 99
Activity 2.2: Knowledge Check Answers
(1 of 2)
1. Name the phases of the development life cycle in order.
Answer:
(1) Gather and analyze the program requirements.
(2) Design the user interface.
(3) Design the program processing objects.
(4) Code the program.
(5) Test the program.
2. The file included in each Android application that provides essential
information such as the minimum API level to the Android device is called
_____.
Answer: the Android Manifest or AndroidManifest.xml
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 100
Activity 2.2: Knowledge Check Answers
(2 of 2)
3. Which Java method initializes an Android Activity?
Answer: onCreate()
4. A part of a program that responds to a user interaction such as tapping an on-
screen button is called a(n) _____.
Answer: event handler
5. Which Java method is necessary to detect a user interaction with the app?
Answer: OnClickListener()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 101
2.10 Correcting Errors in Code
Figure 2-42 Syntax error
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 102
2.11 Saving and Running the Application
• Each time an Android application is tested in the emulator, the programming
design and code are automatically saved
• Click the toolbar's Save All button to save the project
• Click the toolbar’s Run app button to test the application in the emulator
− The first time an app is executed, you will be prompted to choose an
emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 103
Self-Assessment
1. What is your opinion of the Android Studio user interface design tools? Are
there any improvements you'd like to see? Which tools do you anticipate using
most often when creating or updating Android apps?
2. When creating the app project for Chapter 2, which did you enjoy more:
working with the XML layout tools in Studio or writing Java code? How do you
expect to use these UI creation skills in your future career?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 104
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 105
105
Android Bootcamp
for Developers
Using Java, 4e
Chapter 3: Engage! Android
User Input, Variables, and
Operations
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 106
Chapter Objectives (1 of 3)
When you complete this chapter, you will be able to:
• 03.01 Collect user input
• 03.02 Add a Text Number component
• 03.03 Add a string array
• 03.04 Change the background attribute
• 03.05 Set the hint attribute for a Text component
• 03.06 Develop a user interface using a Spinner component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 107
Chapter Objectives (2 of 3)
When you complete this chapter, you will be able to:
• 3.07 Add a prompt and entries to a Spinner component
• 3.08 Code the EditText class for the Text Number component
• 3.09 Code the Spinner component
• 3.10 Instantiate the Button component and variables
• 3.11 Code the getText() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 108
Chapter Objectives (3 of 3)
When you complete this chapter, you will be able to:
• 3.12 Understand arithmetic operations
• 3.13 Code the setText( ) method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 109
3.1 Collecting User Input (1 of 4)
Figure 3-1 Opening screen for
Concert Tickets app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 110
3.1 Collecting User Input (2 of 4)
• Processes required to create this application:
− Define display text using the Translations Editor and strings.xml file
− Set the background color
− Define a Text Number component
− Define a Spinner component
− Convert string data to numeric data
− Perform arithmetic operations with user-entered data
− Display the results, properly formatted, to the user
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 111
3.1 Collecting User Input (3 of 4)
• Simplifying User Input
− The onscreen keyboard is called a soft keyboard
− Input can come in the form of tapping or gestures (using two fingers to pan,
rotate, or zoom)
− The primary design challenge is to simplify the user experience
 Use legible fonts, simplify input, and optimize each device’s capabilities
to maximize user experience
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 112
3.1 Collecting User Input (4 of 4)
• Using Android Text Components
− Text entered via a soft or physical keyboard is the most common type of
mobile input
− Text components can request different input types (e.g., plain text, phone
number, various types of numeric values)
− The component type can constrain the characters available
• User Experience
− User experience refers to all aspects of interaction with a device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 113
3.2 Adding a Text Number Component
(1 of 3)
• You can use the id attribute in the Attributes pane to enter a name that begins
with the prefix tv (for TextView)
− Use descriptive names like tvTickets
• Using the String Table
− String items displayed in the components of an app can be stored in the
string table (strings.xml)
− A string array can be used to store a group of related strings
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 114
3.2 Adding a Text Number Component
(2 of 3)
Figure 3-5 Adding strings in
the Translations Editor
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 115
3.2 Adding a Text Number Component
(3 of 3)
Figure 3-6 String table
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 116
3.3 Adding a String Array (1 of 2)
• A string array defines a string resource of related items in a central location
within strings.xml
• An item defines an individual entry within a string array
• As you type the string array XML code, the Android Studio editor offers
suggestions in a panel that can complete a statement
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 117
3.3 Adding a String Array (2 of 2)
Figure 3-9 The string array
named tvGroup with three
items added in XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 118
3.4 Changing Background Attributes
(1 of 4)
• The Android background feature can help provide an app's individual flair
• The background is white by default
• To change it, set the background attribute in the Attributes pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 119
3.4 Changing Background Attributes
(2 of 4)
Figure 3-10 Changing the
background color
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 120
3.4 Changing Background Attributes
(3 of 4)
Figure 3-11 Title added to
emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 121
3.4 Changing Background Attributes
(4 of 4)
Figure 3-12 Adding a Text
Number component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 122
3.5 Setting the Hint Attribute for a Text
Component (1 of 2)
• A hint is a short description of a component that is visible as gray text (also
called a watermark) inside a Text Number component or any Text input
− Often used to provide guidelines about the input expected in a component
for the user
• You can change the color of the hint by changing the textColorHint attribute
− May be necessary to ensure adequate contrast between the text and
background
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 123
3.5 Setting the Hint Attribute for a Text
Component (2 of 2)
Figure 3-15 Warning symbol
removed from Text Number
component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 124
Activity 3.1: Knowledge Check
1. If a component holds multiple text strings, it is best to store them in a(n)
_____.
2. To change the screen color for an activity in your app, you set the _____.
3. When you see gray text describing what sort of data to enter within a text entry
box in an app, you are looking at a(n) _____.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 125
Activity 3.1: Knowledge Check Answers
1. If a component holds multiple text strings, it is best to store them in a(n)
_____.
Answer: string array
2. To change the screen color for an activity in your app, you set the _____.
Answer: background attribute
3. When you see gray text describing what sort of data to enter within a text entry
box in an app, you are looking at a(n) _____.
Answer: hint
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 126
3.6 Developing a User Interface Using a
Spinner Component
• A Spinner component is similar to a
drop-down list for selecting a single
item from a fixed list
• The Spinner component displays a
prompt with a list of strings called
items
• Figure 3-16 Spinner dialog box
displaying the three concert groups
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 127
3.7 Adding a Prompt and Entries to a Spinner
Component (1 of 6)
• A prompt, which can be used to display instructions at the top of the Spinner
control, is stored in strings.xml and is named prompt
• Using Entries
− The Spinner attribute called entries connects the string array in strings.xml
to the Spinner component so the entries can be displayed in the application
− The spinnerMode attribute sets the component to dialog mode or drop-
down mode
− The layout_width and layout_height attributes set the dimensions
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 128
3.7 Adding a Prompt and Entries to a Spinner
Component (2 of 6)
Figure 3-18 Pick a Resource
dialog box with tvGroup
string array selected
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 129
3.7 Adding a Prompt and Entries to a Spinner
Component (3 of 6)
• Adding the Button, TextView, and ImageView Controls
− ImageView components should have a content description for visually
impaired users
− This description is provided by setting the contentDescription attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 130
3.7 Adding a Prompt and Entries to a Spinner
Component (4 of 6)
Figure 3-20 Adding a Button
component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 131
3.7 Adding a Prompt and Entries to a Spinner
Component (5 of 6)
Figure 3-21 Adding a
TextView component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 132
3.7 Adding a Prompt and Entries to a Spinner
Component (6 of 6)
Figure 3-22 Adding an
ImageView component with a
contentDescription attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 133
3.8 Coding the EditText Class for the Text
Number Component (1 of 2)
• A Java variable is used to contain data that changes during the execution of a
program
• A final variable can be initialized only once
− Attempting to reassign the value results in a compile error when the
application runs
• For the concert tickets app, you insert code to create the tickets variable
containing the extracted EditText class text:
final EditText tickets =
(EditText)findViewById(R.id.tvTickets);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 134
3.8 Coding the EditText Class for the Text
Number Component (2 of 2)
Figure 3-23 EditText code for
Text Number component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 135
3.9 Coding the Spinner Control
Figure 3-24 Spinner
component code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 136
3.10 Instantiating the Button Component and
Variables (1 of 6)
• An OnClickListener waits in the background and listens for the user to click a
button
• Clicking the button activates a Button event, triggering the Button event OnClick
handler to execute the associated code
• Code to instantiate the TextView component to a variable named result is used
to display the result of the ticket price calculation in the concert tickets app:
final TextView result =
((TextView)findViewById(R.id.tvResult));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 137
3.10 Instantiating the Button Component and
Variables (2 of 6)
Figure 3-27 TextView
component code for the
result
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 138
3.10 Instantiating the Button Component and
Variables (3 of 6)
• Declaring Variables
− Two steps are necessary in order to use a variable: (1) declare the variable
and (2) assign a value to the variable
− The declared type of a variable determines which operations are allowed
− Java has eight built-in primitive (simple) types of data
• Primitive Data Types
− Java requires all variables to have a data type
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 139
3.10 Instantiating the Button Component and
Variables (4 of 6)
Type Meaning Range Default Value
byte Often used with arrays –128 to 127 0
short Often used with arrays –32,768 to 32,767 0
int Most commonly used number value –2,147,483,648 to 2,147,483,647 0
long Used for numbers that exceed int –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0
float A 32-bit floating-point number +/–3.40282347 ^38 0
double Most common for decimal values +/–1.79769313486231570 ^308 0
char Used for single characters Characters 0
boolean Used for conditional statements True or false False
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 140
3.10 Instantiating the Button Component and
Variables (5 of 6)
• Primitive Data Types (continued)
− Sample variable initialization and declarations:
double costPerTicket = 89.99;
int numberOfTickets;
double totalCost;
String groupChoice;
• The String Data Type
− The String type is a class and not a primitive data type
− A string can be a character, word, or phrase
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 141
3.10 Instantiating the Button Component and
Variables (6 of 6)
• The String Data Type (continued)
− Most strings used in Java are objects of type String (e.g.,
String groupChoice;)
• Declaring the Variables in the Project
− Typically declared at the beginning of an activity
− Variables must be declared before you can use them
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 142
3.11 Coding the getText() Method
(1 of 2)
• Data stored in the EditText component can be read with the getText() method
• Data is read in as a string, by default
• A Parse class is used to convert strings into numbers
− Parse types include Integer.parseInt(), Float.parseFloat(),
Double.parseDouble(), and Long.parseLong()
• Sample code to extract a string and convert it into an integer:
numberOfTickets =
Integer.parseInt(tickets.getText().toString());
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 143
3.11 Coding the getText() Method
(2 of 2)
• Working with Mathematical Operations
− The ability to perform arithmetic operations is fundamental to many
applications
− Most programs require arithmetic operations to add, subtract, multiply, and
divide numeric data
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 144
Activity 3.2: Knowledge Check
1. A drop-down list for an Android app is created using a(n) _____.
2. The attribute that connects the choices for a drop-down list to their component
is called _____.
3. Which Java keyword prevents a variable from being reassigned?
4. Which Java method reads text stored in an EditText component?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 145
Activity 3.2: Knowledge Check Answers
1. A drop-down list for an Android app is created using a(n) _____.
Answer: Spinner component
2. The attribute that connects the choices for a drop-down list to their component
is called _____.
Answer: entries
3. Which Java keyword prevents a variable from being reassigned?
Answer: final
4. Which Java method reads text stored in an EditText component?
Answer: getText()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 146
3.12 Understanding Arithmetic Operators
(1 of 3)
• Java's arithmetic operators are + (addition), – (subtraction), * (multiplication), /
(division), % (remainder), ++ (increment 1), and -- (decrement 1)
• The order of operations determines the sequence for performing calculations
when multiple operators are included in an expression
− Parentheses; ++ & --; *, /, & %; + & – (left to right)
• Formatting Numbers
− Java includes a class called DecimalFormat that provides patterns for
formatting numbers for output on the Android device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 147
3.12 Understanding Arithmetic Operators
(2 of 3)
Figure 3-30 Calculating and
formatting the ticket cost
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 148
3.12 Understanding Arithmetic Operators
(3 of 3)
• Displaying Android Output: Computed results must be displayed
• Using the getSelectedItem() Method
− The getSelectedItem() method returns the text label of the currently
selected Spinner item
− Sample code that assigns a selected item to a String variable named
groupChoice:
groupChoice = group.getSelectedItem( ).toString( );
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 149
3.13 Coding the setText() Method
• The setText() method displays text in a TextView component
• You can concatenate text and string variable values using +, e.g.,
String mobile = "Android";
String completeSentence = mobile + " is a platform.";
• Sample statement using the setText() method to display text in a TextView
component named result:
result.setText("Cost for " + groupChoice + " is " +
currency.format(totalCost));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 150
Self-Assessment
1. At this point, how comfortable are you using Java classes such as String and
Parse to accomplish the goals of a program? What else would you like to learn
about these Java classes?
2. Consider the Java methods used to code the sample concert ticket
application. How do you anticipate using these methods for your own app
development? What would their most common use cases be for you?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 151
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 152
152
Android Bootcamp
for Developers
Using Java, 4e
Chapter 4: Explore! Icons and
Decision-Making Components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 153
Chapter Objectives (1 of 3)
When you complete this chapter, you will be able to:
• 04.01 Create an Android project with a custom icon
• 04.02 Display the Action bar icon using code
• 04.03 Place a RadioGroup and RadioButtons in Android applications
• 04.04 Determine layouts with the layout:margin attributes
• 04.05 Align components using the foregroundGravity attributes
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 154
Chapter Objectives (2 of 3)
When you complete this chapter, you will be able to:
• 04.06 Add a RadioButton Group
• 04.07 Write code for a RadioButton component
• 04.08 Make decisions with conditional statements
• 04.09 Make decisions using an If statement
• 04.10 Make decisions using an If Else statement
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 155
Chapter Objectives (3 of 3)
When you complete this chapter, you will be able to:
• 04.11 Make decisions using logical operators
• 04.12 Display an Android toast notification
• 04.13 Use the isChecked() method with RadioButtons
• 04.14 Make decisions using nested If statements
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 156
4.1 Creating an Android Project with a
Custom Icon (1 of 12)
• We will be creating an app to convert pounds to kilograms and kilograms to
pounds
• App is designed to be used in a hospital setting to administer medication to
patients based on patient weight
• Hospital scales register pounds, whereas medication dosages are based on
patient weight in kilograms
• Formulas needed:
− Kilograms = pounds * 2.2
− Pounds = kilograms / 2.2
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 157
4.1 Creating an Android Project with a
Custom Icon (2 of 12)
Figure 4-1 Opening screen of
the Medical Calculator
Figure 4-2 Results screen of
the Medical Calculator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 158
4.1 Creating an Android Project with a
Custom Icon (3 of 12)
• Steps to complete the app:
− Create a customized launcher icon
− Use code to add the icon for display in the Action bar
− Define a TextField for the data entry of the weight of the patient
− Define a RadioGroup to select a conversion of pounds to kilograms or
kilograms to pounds
− Display a toast message for data validation
− Convert data so it can be used for arithmetic operations
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 159
4.1 Creating an Android Project with a
Custom Icon (4 of 12)
• Steps to complete the app (continued):
− Perform arithmetic operations on data the user enters
− Display formatted results
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 160
4.1 Creating an Android Project with a
Custom Icon (5 of 12)
• Using the Launcher Icon
− The launcher icon opens the app from the home screen and represents the
app when it is published to Google Play
− A high-quality launcher icon can influence users to purchase your app
− Icons can establish brand identity
− Simple images with clear visual cues have a memorable impact
− Vector-based graphics are best for icon design because these images can
be easily scaled without loss of detail
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 161
4.1 Creating an Android Project with a
Custom Icon (6 of 12)
• Using the Launcher Icon (continued)
− Google Play specifies the size and format of all launcher icons
− Should be saved with the .png file format/extension
− When you publish an app to Google Play, you must provide a 512 × 512-
pixel, high-resolution application icon in the developer console as you upload
your program
 This icon is displayed on the Google Play site to provide a description of
the app and does not replace your launcher icon
− Google Play's naming convention for launcher icons: ic_launcher
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 162
4.1 Creating an Android Project with a
Custom Icon (7 of 12)
Resolution Dots Per Inch (dpi) Size (px)
ldpi (low-density screen) 120 36 × 36
mdpi (medium-density screen) 160 48 × 48
hdpi (high-density screen) 240 72 × 72
xhdpi (extra high-density screen) 320 96 × 96
xxhdpi (extra extra high-density screen) 480 144 × 144
xxxhdpi (extra extra extra high-density screen) 640 192 × 192
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 163
4.1 Creating an Android Project with a
Custom Icon (8 of 12)
• Customizing a Launcher Icon
− Image Asset Studio: Android Studio tool that helps you generate your own
app icons from images
 Generates a set of icons at the appropriate resolution for each pixel
density your app supports
− Use the built-in Image Asset Studio wizard to create these multiple icons and
store them in the correct folders
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 164
4.1 Creating an Android Project with a
Custom Icon (9 of 12)
Figure 4-6 Default launcher
icons in the Asset Studio
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 165
4.1 Creating an Android Project with a
Custom Icon (10 of 12)
Figure 4-7 Custom launcher
icons
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 166
4.1 Creating an Android Project with a
Custom Icon (11 of 12)
Figure 4-8 Confirmed icon
path in Asset Studio displayed
in res/mipmap folders
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 167
4.1 Creating an Android Project with a
Custom Icon (12 of 12)
Figure 4-9 Resized custom
icons and their pathways in
the mipmap folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 168
4.2 Displaying the Action Bar Icon Using
Code (1 of 4)
• An Action bar icon is considered a logo that represents what the program’s
function is in a single glance
− For example, the medical symbol conveys the purpose and identity of the
app
• Supporting code needed in MainActivity.java to display the image in the Action
bar:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 169
4.2 Displaying the Action Bar Icon Using
Code (2 of 4)
Figure 4-11 Icon displayed in
running app in the emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 170
4.2 Displaying the Action Bar Icon Using
Code (3 of 4)
• String Table
− String resources are stored within the /res/values/strings.xml file
− Any strings you add to the strings.xml file are accessible within your
application
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 171
4.2 Displaying the Action Bar Icon Using
Code (4 of 4)
Figure 4-12 Translations
Editor with new strings in the
String table
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 172
4.3 Placing a RadioGroup and RadioButtons
in Android Applications (1 of 2)
• RadioButton components are used to select or deselect an option
− Can be arranged horizontally or vertically
− Have a label defined by the Text attribute
− Checked attribute can be set to true or false
− Typically used together in a RadioGroup container
 Only one RadioButton in the group can be selected at a time
− It saves time to offer a default selection (checked = true) for the option that is
used most often
172
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 173
4.3 Placing a RadioGroup and RadioButtons
in Android Applications (2 of 2)
• Changing the Text Color of RadioButton and CheckBox Components
− Hexadecimal color code: a triplet of three colors
 Example: #FF0000 is a true red
 Color system used by the Android platform
− Codes range from 00 to FF (00 = none, FF = full)
− Codes are identified by a pound sign, followed by the red, green, and blue
values contained in the color
173
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 174
4.4 Determining Layouts with the
layout:margin Attributes
• You can change a component's alignment after placing it on the UI by adjusting
its Gravity settings
• The layout:margin attribute allows for more flexibility in controlling your layout
− You can change all margins equally or change the left, top, right, and bottom
margins individually
• Set density-independent pixel (dp) values instead of “eyeballing” to create equal
spaces around components
• Using the same specified margins creates a symmetrical layout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 175
Activity 4.1: Knowledge Check
(1 of 2)
1. What is an app's opening icon called?
2. What tool can you use to generate a launcher icon from an image?
3. RadioButton components are typically used together in a(n) _____.
4. What is #FF0000?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 176
Activity 4.1: Knowledge Check
(2 of 2)
5. What settings are used to change the alignment of an object?
6. Which attribute can you use to set the spacing around each side of an object
individually?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 177
Activity 4.1: Knowledge Check Answers
(1 of 2)
1. What is an app's opening icon called?
Answer: The launcher icon
2. What tool can you use to generate a launcher icon from an image?
Answer: Image Asset Studio
3. RadioButton components are typically used together in a(n) _____.
Answer: RadioGroup
4. What is #FF0000?
Answer: The hexadecimal color code for a true red color
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 178
Activity 4.1: Knowledge Check Answers
(2 of 2)
5. What settings are used to change the alignment of an object?
Answer: Gravity settings
6. Which attribute can you use to set the spacing around each side of an object
individually?
Answer: layout:margin attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 179
4.5 Aligning Components Using the
foregroundGravity Attributes
• A component's gravity can be changed using the Attributes pane or with a
toolbar shortcut
• The foregroundGravity tool changes the linear alignment of a component
• Layout gravity is similar to the alignment feature in Microsoft Office that allows a
component to snap to the left, center, right, top, or bottom
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 180
4.6 Adding the RadioButton Group and Other
Components (1 of 5)
• When naming a RadioButton with the id attribute, use a name that begins with
the prefix rb, which represents a radio button in the code
• Completing the User Interface
− Remember to create a clean layout and use the entire screen effectively
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 181
4.6 Adding the RadioButton Group and Other
Components (2 of 5)
Figure 4-15 Text Number
component and
foregroundGravity attribute
to center hint text
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 182
4.6 Adding the RadioButton Group and Other
Components (3 of 5)
Figure 4-16 RadioGroup
component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 183
4.6 Adding the RadioButton Group and Other
Components (4 of 5)
Figure 4-17 Two RadioButton
components with first
RadioButton checked
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 184
4.6 Adding the RadioButton Group and Other
Components (5 of 5)
Figure 4-20 TextView
component to display results
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 185
4.7 Coding a RadioButton Component
(1 of 3)
• Java syntax to reference each of the RadioButton components using the
findViewById() method:
final RadioButton lbToKilo = (RadioButton)
findViewById(R.id.rbLbToKilo);
final RadioButton kiloToLb = (RadioButton)
findViewById(R.id.rbKiloToLb);
• Coding the Button Component
− The Button component detects user interaction using an event listener
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 186
4.7 Coding a RadioButton Component
(2 of 3)
Figure 4-22 EditText and
RadioButtons referenced
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 187
4.7 Coding a RadioButton Component
(3 of 3)
Figure 4-24 OnClickListener
for the Button component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 188
4.8 Making Decisions with Conditional
Statements
• Decision structure: a fundamental computer programming structure that tests
a condition and then follows a path specified for the result
• A conditional statement tests a condition and returns a result of either true or
false
− Boolean operators have two values, which are either true (1) or false (0)
− When you compare values, Java returns a Boolean value of true or false
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 189
4.9 Making Decisions Using an If Statement
• An If statement tests a condition and executes the statement(s) between its
curly braces only when the condition is true
• Syntax for simplest form of the Java If statement:
if (condition) {
//Statements completed if true
}
• If the condition is false, program execution continues with the statements after
the closing brace
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 190
4.10 Making Decisions Using an If Else
Statement (1 of 3)
• An If Else statement executes one set of instructions if a condition is true and
another set of instructions if the condition is false
• Java syntax for the If Else statement:
if (condition) {
//Statements completed if condition is true
} else {
//Statements completed if condition is false
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 191
4.10 Making Decisions Using an If Else
Statement (2 of 3)
Relational Operator Meaning Example Resulting Condition
== Equal to 6 == 6 True
!= Not equal to 4 != 7 False
> Greater than 3 > 2 True
< Less than 8 < 1 False
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 9 <= 6 False
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 192
4.10 Making Decisions Using an If Else
Statement (3 of 3)
• Relational Operators
− Relational operators are used in conditional statements to express the
relationship between the numbers being tested
− Java strings are compared using the equals() method
− The compareTo() method is used to determine the alphabetical order of
strings
 Returns a negative integer if the first string precedes the second
 Returns 0 if they are equal
 Returns a positive integer if the first string follows the second
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 193
4.11 Making Decisions Using Logical
Operators
• When more than one condition is included in an If statement, the conditions are
called a compound condition
• Java logical operators are needed to control the flow of execution with
compound conditions
− && means and—all Boolean conditions must be true
− || means or—at least one Boolean condition must be true
− ! means not—it reverses the meaning of a Boolean condition
• Data validation: the practice of checking the integrity, accuracy, and structure of
a program before it is used for a business operation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 194
4.12 Displaying an Android Toast Notification
• A toast notification communicates messages to the user unobtrusively
− Message slides upward into view like toast popping out of a toaster
• The toast notification code uses a Toast object and the makeText() method with
three parameters: context (activity name), message text, and duration of display
• Java syntax: Toast toast = Toast.makeText(context, text,
duration).show();
− Duration is LENGTH_SHORT or LENGTH_LONG
− The show() method displays the Toast object
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 195
4.13 Using the isChecked() Method with
RadioButton Components
• The isChecked() method determines if a RadioButton (or CheckBox) object
has been selected
• Example syntax:
if (lbToKilo.isChecked) {
//Statements completed if condition is true (Boolean
value)
} else {
//Statements completed if condition is false (Boolean
value)
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 196
Activity 4.2: Knowledge Check
1. What kind of statement is used to test a radio button's state?
2. What Java method is used to determine whether or not a radio button has been
selected?
3. When you use a(n) _____ statement in a decision structure, code is executed for
either a true or a false result.
4. What do we use to compare strings to see if they are equal?
5. What are the three common logical operators?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 197
Activity 4.2: Knowledge Check Answers
1. What kind of statement is used to test a radio button's state?
Answer: A conditional statement
2. What Java method is used to determine whether or not a radio button has been
selected?
Answer: isChecked()
3. When you use a(n) _____ statement in a decision structure, code is executed for
either a true or a false result.
Answer: If Else
4. What do we use to compare strings to see if they are equal?
Answer: Java equals() method
5. What are the three common logical operators?
Answer: && (And), || (Or), ! and (Not)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 198
4.14 Using Nested If Statements (1 of 5)
• When you place an If statement within another If statement, the inner If
statement is said to be nested within the outer If statement
• Coding the Button Event
− The syntax Double.parseDouble converts input to a Double data type
− Integer.parseInt converts input to an Integer data type
− The DecimalFormat object is used to format data for display
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 199
4.14 Using Nested If Statements (2 of 5)
Figure 4-26 DecimalFormat
code used to round to the
nearest tenth
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 200
4.14 Using Nested If Statements (3 of 5)
• Coding the Nested If Statements
− In the Medical Calculator app, nested If statements are used
 Outer If statements determine the conversion option selected
 Inner If Else statements display the result only if the weight is within 500
pounds or 225 kilograms (the If branch)
 If the weight is out of this range, a toast notification is displayed instead
(this is the Else branch)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 201
4.14 Using Nested If Statements (4 of 5)
Figure 4-31 Completed code
for If Else statements
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 202
4.14 Using Nested If Statements (5 of 5)
Figure 4-32 Running app and
toast notification
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 203
Self-Assessment
1. How much attention do you pay to the launcher icons for apps that you use?
Do you think that launcher icons have impacted your choices regarding which
apps to download?
2. What previous experience do you have designing and coding decision
structures? Would you have coded the logic for the Medical Calculator app
differently? If so, explain why.
3. Describe how you expect to use toast notifications in apps that you develop.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 204
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 205
205
Android Bootcamp
for Developers
Using Java, 4e
Chapter 5: Investigate! Android
Lists, Arrays, Switch Statements,
and Web Browsers
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 206
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 05.01 Create an Android project using a list
• 05.02 Add a ListView layout with XML code
• 05.03 Create an array
• 05.04 Initialize a ListView layout component
• 05.05 Display an array in the ListView with an ArrayAdapter
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 207
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 05.06 Call the setOnItemClick() method
• 05.07 Create a decision structure using a Switch statement
• 05.08 Describe the purpose of intents in Android
• 05.09 Launch a browser from an Android device
• 05.10 Test an application with multiple decisions
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 208
5.1 Creating an Android Project Using a List
(1 of 6)
• Lists are one of the most common
designs in mobile apps
− Scrollable
− Selectable
− Programmable to bring up the
next activity (screen)
• Figure 5-1 The City Guide Android
app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 209
5.1 Creating an Android Project Using a List
(2 of 6)
• Steps to complete the City Guide app:
1. Create a list using a ListView component.
2. Define an array to establish the items of the list.
3. Add the images used in the project.
4. Define an XML file to design a list.
5. Code a Switch decision structure to handle the selection of items.
6. Open an Android web browser to display a specified Uniform Resource Identifier
(URI).
7. Create multiple classes and XML layout files to display pictures of attractions.
209
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 210
5.1 Creating an Android Project Using a List
(3 of 6)
• Beginning the City Guide App
− The opening screen of the City Guide app contains a vertical list of
attractions
− Selecting an item opens up a related web page or an image of the attraction
− The TableLayout view is similar to the ListView, but ListView allows you to
select each list row for further action
− With a ListView component, scrollbar is automatically displayed if there are
more items than the screen can display
210
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 211
5.1 Creating an Android Project Using a List
(4 of 6)
Figure 5-4 Application
information for the new
Android project
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 212
5.1 Creating an Android Project Using a List
(5 of 6)
Figure 5-5 Asset Studio dialog
box
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 213
5.1 Creating an Android Project Using a List
(6 of 6)
Figure 5-6 Code to display
launcher icon
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 214
5.2 Adding a ListView Layout with XML Code
(1 of 2)
• Two ways to design a layout:
− Drag and drop components from the Palette into the emulator window
− Code the activity_main.xml file using XML code in the Layout Editor
• Studio provides an auto-complete feature to assist in writing XML
• For the City Guide project, the ListView component will be added by modifying
the activity_main.xml file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 215
5.2 Adding a ListView Layout with XML Code
(2 of 2)
Figure 5-8 ListView layout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 216
5.3 Creating an Array (1 of 2)
• Array: a container object that holds a fixed number of values of a single type
• An array variable can store more than one value
− You can avoid assigning a separate variable for each item in a list for a ListView
component
• Each individual item in an array that contains a value is called an element
• Arrays provide access to data by using a numeric index, or subscript
− Elements of the attraction array are accessed as attraction[0],
attraction[1], attraction[2], etc.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 217
5.3 Creating an Array (2 of 2)
• Declaring an Array
− Declaration includes the array's data type and its name
− Sample arrays of numeric and character values:
int[] age={18,21,38,88};
double[] weather={72.3, 65.0, 25.7, 99.5};
char[] initials={'P','N','D'};
− Array of strings for the City Guide's ListView component:
String[] attraction = new String[]{"Art Institute of
Chicago", "Magnificent Mile", "Willis Tower", "Chicago
Bean", "Water Tower"};
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 218
5.4 Initializing a ListView Layout Component
(1 of 2)
• You use the Android ListView layout to display the array elements in a scrollable
list
• The list items are automatically inserted in the ListView component using a Java
Adapter that pulls content from a source such as an array or database
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 219
5.4 Initializing a ListView Layout Component
(2 of 2)
Figure 5-11 ListView layout
defined
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 220
5.5 Displaying an Array in the ListView with
an ArrayAdapter (1 of 3)
• An adapter provides a data model for the layout of a list and for converting the
array data into list items
• The ListView and ArrayAdapter work together to display a list
• The ArrayAdapter<String> supplies the String array data to the ListView
• The three parameters that follow ArrayAdapter refer to:
− The this class
− A generic layout (simple_list_item_1 for the City Guide)
− The array of list items
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 221
5.5 Displaying an Array in the ListView with
an ArrayAdapter (2 of 3)
• Adapter code for the City Guide app:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
attraction);
listView.setAdapter(adapter);
• Adding the Images to the Resources Folder
− Place images for the app into the res/drawable folder
− Remember to follow copyright laws that apply to any copyrighted images you
use
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 222
5.5 Displaying an Array in the ListView with
an ArrayAdapter (3 of 3)
• Adding the String Table
− The City Guide app needs three strings for the ImageView component
descriptions
− These are added to the strings.xml file in the resvalues folder using the
Translations Editor
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 223
Activity 5.1: Knowledge Check
1. What is the name of the component that makes it easy to display a list of
items?
2. What are the individual items in an array called?
3. What does an adapter do?
4. Where do images need to be stored?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 224
Activity 5.1: Knowledge Check Answers
1. What is the name of the component that makes it easy to display a list of
items?
Answer: ListView component
2. What are the individual items in an array called?
Answer: elements
3. What does an adapter do?
Answer: It provides a data model for the layout of the list and for converting
the array data into list items
4. Where do images need to be stored?
Answer: In the res/drawable folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 225
5.6 Calling the setOnItemClick() Method
• setOnItemClick() is called when an item from the list is selected
• When an attraction in the list is selected, the position of the item is passed from
setOnItemClick() and evaluated with a decision structure
− The position parameter is assigned an integer value based on the index of
the array item selected (0, 1, 2, etc.)
• Code calling this method and onItemClick() method stub:
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 226
5.7 Creating a Decision Structure with a
Switch Statement (1 of 3)
• Switch statement: a decision structure that allows you to choose from many
statements based on an integer or single character (char) input
• Switch statement syntax:
switch(position){
case 0:
//statements that are executed if position == 0
break;
case 1:
//statements that are executed if position == 1
break;
default:
//statements that are executed if position != any of the
cases
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 227
5.7 Creating a Decision Structure with a
Switch Statement (2 of 3)
• The switch keyword is followed by:
− An integer expression in parentheses
− Its cases, all enclosed in braces
• The case keyword is followed by a colon and a value
• Typically, the statement within a case ends with a break statement
− The break statement exits the Switch decision structure and continues with
the next statement
• If there is no matching case value, the (optional) default option is executed
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 228
5.7 Creating a Decision Structure with a
Switch Statement (3 of 3)
Figure 5-18 Case statements
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 229
5.8 Understanding Android Intents
• A browser is launched with Android code using an intent
• Android intents send and receive activities and services
− They allow apps to talk to each other in a very simple way
• When the City Guide app sends an intent, the browser app handles the request
and opens the website
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 230
5.9 Launching a Browser from an Android
Device (1 of 6)
• Android phones have a built-in browser with an intent filter that accepts intent
requests from other apps
− The intent sends the browser a URI (Uniform Resource Identifier), a string
that identifies web resources
− URL (Uniform Resource Locator): a website address
− A URI is a URL with additional information necessary for gaining access to
the resources required for posting a web page
• ACTION_VIEW: a generic action you can use to send any request to get the
most reasonable action to occur
− The most common action performed on data
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 231
5.9 Launching a Browser from an Android
Device (2 of 6)
• Sample code for launching websites in an Android browser:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
("http://artic.edu")));
• Adding Multiple Class Files
− Multiple classes are needed to display images on the screen when the user selects
Willis Tower, Chicago Bean, or Water Tower on the opening ListView component
− Classes must begin with a capital letter
− When you add a class by creating a new activity, a coordinating XML layout file with
the same name but a lowercase beginning letter is automatically created
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 232
5.9 Launching a Browser from an Android
Device (3 of 6)
• Designing XML Layout Files
− For the City Guide app, XML layout files must be designed to display an
ImageView component with an image source file
− To do this, you can drag an ImageView component into the emulater
− You can add its id attribute, adjust its constraint handles, and select a
description from the string table for its contentDescription attribute
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 233
5.9 Launching a Browser from an Android
Device (4 of 6)
Figure 5-25 The
activity_bean.xml file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 234
5.9 Launching a Browser from an Android
Device (5 of 6)
• Coding the Multiple Class Files
− The City Guide app must launch the class files when the user selects an
item from the ListView component
− A startActivity() method for each case in the Switch statement opens the next
Activity, which in turn launches the appropriate XML layout that displays an
image
− Sample call to startActivity():
startActivity(new Intent(MainActivity.this,
Bean.class));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 235
5.9 Launching a Browser from an Android
Device (6 of 6)
Figure 5-27 Complete code
for MainActivity.java class
[part b]
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 236
Activity 5.2: Knowledge Check
1. What Java method, which is called when an item in a ListView component is
selected, passes the position of the item for evaluation?
2. A Switch statement attempts to match an integer or character to one of its _____.
3. An Android intent that launches a browser identifies web resources by sending the
browser a(n) _____.
4. What is the most common action performed on data in an Android app?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 237
Activity 5.2: Knowledge Check Answers
1. What Java method, which is called when an item in a ListView component is
selected, passes the position of the item for evaluation?
Answer: setOnItemClick()
2. A Switch statement attempts to match an integer or character to one of its _____.
Answer: cases
3. An Android intent that launches a browser identifies web resources by sending the
browser a(n) _____.
Answer: URI (Uniform Resource Identifier)
4. What is the most common action performed on data in an Android app?
Answer: ACTION_VIEW
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 238
5.10 Testing an Application with Multiple
Decisions
• Make sure all the fields can gracefully handle any click or any value entered in any
Android app
• Click Run on the menu bar, then select Run to test the app in the emulator
• Testing an Android app is called usability testing
• In addition to the traditional tests of navigation and ease of use, Section 508
compliance is a third component to be tested
− The 1998 Amendment to Section 508 of the Rehabilitation Act spells out
accessibility requirements for individuals with certain disabilities
− For more details, refer to General Services Administration: Section 508 Report
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 239
Self-Assessment
1. How much of the Java code in the City Guide app project presented in
Chapter 5 was familiar versus unfamiliar to you? Describe your experience
working through the coding project and identify any related coding skills that
you would like to practice more.
2. Describe any previous coding experience you have with the Switch-case
decision structure. In what situations do you expect to use Switch statements
in your own coding projects?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 240
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 241
241
Android Bootcamp
for Developers
Using Java, 4e
Chapter 6: Jam! Implementing
Audio in Android Apps
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 242
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 06.01 Create an Android project using a splash screen
• 06.02 Launch the splash screen before the MainActivity
• 06.03 Design a TextView component with a background image
• 06.04 Create a timer
• 06.05 Understand the life and death of an Activity
• 06.06 Assign class variables
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 243
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 06.07 Play music
• 06.08 Create a raw folder for music files
• 06.09 Play music with a MediaPlayer method
• 06.10 Start and resume music playback
• 06.11 Change the text attribute of a component
• 06.12 Change the visibility of a component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 244
6.1 Creating an Android Project Using a
Splash Screen (1 of 5)
Figure 6-1 Aloha
Music Android app
Figure 6-2 Music
played in the
Android app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 245
6.1 Creating an Android Project Using a
Splash Screen (2 of 5)
• The Aloha Music app displays a splash screen (opening screen) for five seconds, then opens a
second screen automatically
• Steps to complete the app:
− Create a splash screen with a timer.
− Design a TextView control with a background image.
− Initialize a TimerTask and a timer.
− Launch a second Activity.
− Design a second XML layout.
− Add music files to the raw folder.
− Initialize the MediaPlayer class.
− Play and pause music with a Button component.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 246
6.1 Creating an Android Project Using a
Splash Screen (3 of 5)
• Beginning the Aloha Music App
− Many app splash screens include the program name, a brand logo, and/or
the author's name
− Android initializes its resources and loads necessary files while the splash
screen is displayed
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 247
6.1 Creating an Android Project Using a
Splash Screen (4 of 5)
Figure 6-3 Setting up the
Aloha Music project in the
New Project window
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 248
6.1 Creating an Android Project Using a
Splash Screen (5 of 5)
Figure 6-4 Adding the Splash
Activity (splash screen)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 249
6.2 Launching the Splash Screen before the
MainActivity (1 of 3)
• By default, the MainActivity launches first
• To launch the SplashActivity (splash screen) first, you must modify the Android
Manifest file
− Move the first launch activity intent from MainActivity to SplashActivity
− Set android:exported property to "true"
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 250
6.2 Launching the Splash Screen before the
MainActivity (2 of 3)
Figure 6-5 Updating the
Android Manifest file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 251
6.2 Launching the Splash Screen before the
MainActivity (3 of 3)
Figure 6-6 Launching the
Splash Activity first in the
Android Manifest file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 252
6.3 Designing a TextView Component with a
Background Image (1 of 3)
• You use the TextView component's background attribute to specify a background image
− Can display an image or a solid-color fill
• The margins and gravity properties are used to place the text within the component
• For the Aloha Music app, you must:
− Add images to the drawable folder
− Enter text strings into the string table using the Translations Editor
− Add a TextView Component with a background image to the XML layout file for the
splash screen
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 253
6.3 Designing a TextView Component with a
Background Image (2 of 3)
Figure 6-8 Translations Editor
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 254
6.3 Designing a TextView Component with a
Background Image (3 of 3)
Figure 6-9 The
activity_splash.xml file
displays a TextView
component with a
background image
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 255
6.4 Creating a Timer (1 of 5)
• A timer in Java can:
− Execute a one-time task such as displaying an opening splash screen
− Perform a continuous process such as a morning wake-up call set to run at
regular intervals
• Timers use two Java classes, TimerTask and Timer
• Each time a timer runs, it runs in a single thread: a single sequential flow of
control within a program
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 256
6.4 Creating a Timer (2 of 5)
• The first step in creating a timer is creating a TimerTask object:
TimerTask task = new TimerTask( ) {
@Override
public void run() {
// TODO Auto-generated method stub
}
• The TimerTask invokes a scheduled timer
• The TimerTask must implement a run() method that is called by the timer when
the task is scheduled for execution
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 257
6.4 Creating a Timer (3 of 5)
Figure 6-11 The run() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 258
6.4 Creating a Timer (4 of 5)
• Scheduling a Timer
− After a reference is included to the TimerTask class, a timer must be scheduled
− A delay timer is scheduled in milliseconds using the Timer class:
Timer opening = new Timer();
opening.schedule(task,5000);
 Line 1: the object named opening initializes a new instance of the Timer class
 Line 2: the schedule() method of the Timer class is called, passing in the name
of the variable that was initialized for the Timer class and the number of
milliseconds
 5,000 milliseconds = about 5 seconds
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 259
6.4 Creating a Timer (5 of 5)
Figure 6-13 Timer scheduled
for 5 seconds
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 260
6.5 Understanding the Life and Death of an
Activity (1 of 7)
• Each activity has a life cycle—a series of actions from the beginning of an
Activity until its end
• When the activity begins, we use the onCreate() method
− Initializes the user interface with an XML layout
− Sets up the resources required to perform the Activity
• When the activity ends, we use the onDestroy() method
− Releases the Activity's resources to free up memory
• Four states of an Activity: active, paused, stopped, or dead
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 261
6.5 Understanding the Life and Death of an
Activity (2 of 7)
Method Description
onCreate() The onCreate() method begins each Activity. This method also provides a Bundle containing
the Activity’s previously frozen state, if it had one.
onRestart() If the Activity is stopped, onRestart() begins the Activity again. If this method is called, it
indicates your Activity is being redisplayed to the user from a stopped state. The onRestart()
method is always followed by onStart().
onStart() If the Activity is hidden, onStart() makes the Activity visible.
onResume() The onResume() method is called when the user begins interacting with the Activity. The
onResume() method always follows onPause().
onPause() This method is called when an Activity is about to resume.
onStop() This method hides the Activity.
onDestroy() This method destroys the Activity. Typically, the finish() method (part of the onDestroy()
method) is used to declare that the Activity is finished; when the next Activity is called, the
method releases all the resources from the first Activity.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 262
6.5 Understanding the Life and Death of an
Activity (3 of 7)
• The finish() method is part of the onDestroy() method
− Called when the Activity is completed and should be closed
− Typically occurs directly before another Activity is launched
• In Figure 6-14:
− Rectangles represent methods you can implement to perform operations when the
Activity moves between states
− Colored ovals are the possible major states of the Activity
• In the Aloha Music app, onDestroy() is called from within the timer task's run() method
to destroy the SplashActivity before launching the second Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 263
6.5 Understanding the Life and Death of an
Activity (4 of 7)
Figure 6-14 Android life cycle
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 264
6.5 Understanding the Life and Death of an
Activity (5 of 7)
Figure 6-15 After the finish( )
method is called
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 265
6.5 Understanding the Life and Death of an
Activity (6 of 7)
• Launching the Next Activity
− An intent must request that the next Activity be launched
− For the Aloha Music app, the following line is added to the run() method after the
call to finish():
startActivity(new Intent(SplashActivity.this,
MainActivity.class));
• Designing the activity_main.xml File
− A second XML layout file is displayed when the onCreate() method is called within
MainActivity.java
− For the Aloha Music app, MainActivity.java uses the default Relative layout with two
ImageView and Button components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 266
6.5 Understanding the Life and Death of an
Activity (7 of 7)
Figure 6-19 The
activity_main.xml layout is
complete
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 267
Activity 6.1: Knowledge Check
1. What do you call an app's opening screen that displays a logo for a few
seconds?
2. Which type of object invokes a scheduled timer?
3. How many milliseconds equal approximately 1 second?
4. What are the four possible states of an Activity?
5. Which Android life cycle method will hide the Activity?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 268
Activity 6.1: Knowledge Check Answers
1. What do you call an app's opening screen that displays a logo for a few seconds?
Answer: Splash Screen
2. Which type of object invokes a scheduled timer?
Answer: TimerTask
3. How many milliseconds equal approximately 1 second?
Answer: 1,000
4. What are the four possible states of an Activity?
Answer: active, paused, stopped, or dead
5. Which Android life cycle method will hide the Activity?
Answer: onStop()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 269
6.6 Assigning Class Variables (1 of 3)
• Local variables are declared by variable declaration statements within a
method
− Local variables effectively cease to exist when method execution is complete
• The scope of a variable refers to the variable’s visibility within a class
• Variables accessible from anywhere in a class have global scope
− When a variable is needed in multiple methods in a class, a global variable
is used
− Global scope variables are called class variables in Java
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 270
6.6 Assigning Class Variables (2 of 3)
• For the Aloha Music app, class variables are defined for a Button, a
MediaPlayer, and an integer variable (playing) for each Button component's
onClick() method
• The onCreate() method opens the XML layout and defines two Button
components
− Each button has a setOnClickListener that implements the
Button.OnClickListener, creating an instance of OnClickListener and calling
the onClick() method, when the button is tapped
− The onClick() method responds to the user's action by playing or pausing the
song
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 271
6.6 Assigning Class Variables (3 of 3)
Figure 6-23 Inserting the
second Button
OnClickListener stub
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 272
6.7 Playing Music
• Android phones and tablets include built-in music players
• Android includes a MediaPlayer class that can play both audio and music files
• Android allows you to play audio and video from several types of data sources:
media files stored in the app's resources, stand-alone files, an SD (Secure
Digital) memory card, a data stream from the Internet
• .mp3 is the most common supported audio file type, but .wav, .ogg, and .midi
are typically supported by most Android hardware
• Codec: a computer technology used to compress and decompress audio and
video files
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 273
6.8 Creating a Raw Folder for Music Files
• Music files are typically stored in a new resource directory called raw
− A subfolder created in the res folder
• The raw resource directory must be created before music files are added to the
project
• In Android Studio, right-click the res folder and select New, then Android
resource directory to add the raw folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 274
6.9 Playing Music with a MediaPlayer Method
(1 of 2)
• A MediaPlayer class provides the methods to control audio playback on
Android devices
− Provides access to built-in MediaPlayer services for playing audio and video
files
• For the Aloha Music app, two MediaPlayer class variables are declared and an
instance of the MediaPlayer class is assigned to each
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 275
6.9 Playing Music with a MediaPlayer Method
(2 of 2)
Figure 6-26 MediaPlayer
instance statements
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 276
6.10 Starting and Resuming Music Playback
(1 of 2)
• The playback state of the MediaPlayer determines whether an audio file is playing
− start() begins media playback
− pause() pauses media playback
− stop() stops media playback
• Android does not have a method for determining the current state of the MediaPlayer
• For the Aloha Music app, an integer variable named playing is used to track the state
− A Switch decision structure changes the value of playing each time the user taps
the playback/pause button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 277
6.10 Starting and Resuming Music Playback
(2 of 2)
Figure 6-27 Variable named
playing is set to 0
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 278
6.11 Changing the Text Attribute of a
Component (1 of 2)
• When a Button component is used as a toggle button, its text should be
changed to correspond with its functionality when it is tapped
• You can change the text attribute using Java code by calling the setText()
method:
button1.setText("Pause Ukulele Song");
• For the Aloha Music app, this method is added to the case statements of each
Button component's Switch decision structure
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 279
6.11 Changing the Text Attribute of a
Component (2 of 2)
Figure 6-28 The setText( )
method changes the button
text in both case statements
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 280
6.12 Changing the Visibility of a Component
(1 of 4)
• Visibility attribute: the Java attribute that determines whether a component is
displayed on the emulator
− Set to be visible by default
− Code to hide a component:
button2.setVisibility(View.INVISIBLE);
− Code to display a component:
button2.setVisibility(View.VISIBLE);
• For the Aloha Music app, calls to setVisibility() are added to the Switch decision
structure to make the opposing play button disappear
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 281
6.12 Changing the Visibility of a Component
(2 of 4)
Figure 6-30 The setVisibility()
method changes the visibility
of the Button component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 282
6.12 Changing the Visibility of a Component
(3 of 4)
Figure 6-31 Completed code
for MainActivity.java
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 283
6.12 Changing the Visibility of a Component
(4 of 4)
• Running and Testing the Application
− Click Run  Run on the Android Studio menu bar to save and test the
application in the emulator
− The splash screen should display for five seconds
− Then the activity_main layout screen should open
− Test both buttons to ensure they operate as expected
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 284
Activity 6.2: Knowledge Check
1. Which kind of variable is visible to all methods within a class?
2. What goes in the raw folder?
3. Why do we need Switch statements in the Aloha Music app?
4. What are the two possible values for the visibility attribute?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 285
Activity 6.2: Knowledge Check Answers
1. Which kind of variable is visible to all methods within a class?
Answer: global variable or class variable
2. What goes in the raw folder?
Answer: audio or video files
3. Why do we need Switch statements in the Aloha Music app?
Answer: Using Switch statements for the Button components allows us to
easily perform the set of actions appropriate for the current state of the
MediaPlayer (the case) when a button is tapped.
4. What are the two possible values for the visibility attribute?
Answer: visible and invisible
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 286
Self-Assessment
1. As an app consumer, have you noticed splash screens and/or background
music in apps that you frequently use? If so, what do you think of these
features?
2. To what extent, and in what contexts, do you anticipate using the TimerTask,
Timer, and MediaPlayer classes in apps that you develop professionally?
3. Attempt to map the user flow of one of your favorite apps to the Android life
cycle depicted in Figure 6-14. How easy or difficult is it to recognize the app's
activities and follow their state changes?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 287
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 288
288
Android Bootcamp
for Developers
Using Java, 4e
Chapter 7: Reveal! Displaying
Pictures in a GridView
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 289
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 07.01 Create an Android project using a GridView component
• 07.02 Add a GridView to display a two-dimensional grid of images
• 07.03 Add the ImageView component in XML code
• 07.04 Create an array for images
• 07.05 Instantiate the GridView and ImageView components
• 07.06 Use a setAdapter with an ImageAdapter
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 290
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 07.07 Code the OnItemClickListener
• 07.08 Code a custom toast notification
• 07.09 Display the setImageResource image
• 07.10 Define a Context resource
• 07.11 Determine the length of an array
• 07.12 Code the getView() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 291
7.1 Creating an Android Project Using a
GridView Component (1 of 3)
• The GridView layout tool shows items in a two-dimensional scrolling grid
• Endangered Species app
− Users can scroll an image grid by flicking their fingers across a horizontal list
of thumbnail-sized pictures
− To view a larger image of one of the endangered animals, users can tap a
thumbnail
− A toast notification is used to display the species number for the selected
image
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 292
7.1 Creating an Android Project Using a
GridView Component (2 of 3)
Figure 7-2 Panda image
selected in the grid
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 293
7.1 Creating an Android Project Using a
GridView Component (3 of 3)
• To create this app, you must:
− Add a GridView component and an
ImageView component to the
emulator
− Update the XML code for an
ImageView component not linked to a
particular image
− Place six images in a drawable folder
− Define an array to hold the image
files
− Instantiate the GridView and
ImageView components
− Create an ImageAdapter class
− Display a custom toast message
− Display the selected image
− Customize the ImageAdapter class
− Define the layout using the getView()
method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 294
7.2 Adding a GridView to Display a Two-
Dimensional Grid of Images (1 of 4)
• A View container is a rectangular area of the screen that displays an image or text
object
− Can include layouts such as GridView, RadioGroup, ScrollView, TabHost, and
ListView
• The GridView container displays a horizontal list of objects
− This layout can be used to build applications that show elements in a table or grid
− Photos in a grid can be sized as thumbnail images or full-screen images
− Photos can be stored in the drawable folders, in a phone’s storage, or on a website
such as Picasa
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 295
7.2 Adding a GridView to Display a Two-
Dimensional Grid of Images (2 of 4)
GridView Attribute Name Description
android:columnWidth Specifies the fixed width for each column of the grid
android:numColumns Defines the number of columns in the grid
android:horizontalSpacing Defines the default horizontal spacing between columns in the grid
android:verticalSpacing Defines the default vertical spacing between rows in the grid
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 296
7.2 Adding a GridView to Display a Two-
Dimensional Grid of Images (3 of 4)
Figure 7-4 GridView
component in the emulator
for the Endangered Species
project
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 297
7.2 Adding a GridView to Display a Two-
Dimensional Grid of Images (4 of 4)
Figure 7-6 Customized
GridView XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 298
7.3 Adding the ImageView Component in XML
Code (1 of 4)
• Typically, you add an ImageView component by dragging it onto the emulator
• If an image should appear in the ImageView component only after a user event,
you can prevent an image from being automatically assigned to the component
by entering the XML code for it instead
− The component is coded in activity_main.xml
− The source display is coded in MainActivity.java
• The ImageView's content description is entered into the String table
• The image files for display are placed in the drawable folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 299
7.3 Adding the ImageView Component in XML
Code (2 of 4)
Figure 7-7 ImageView XML
code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 300
7.3 Adding the ImageView Component in XML
Code (3 of 4)
Figure 7-8 Adding a String
value
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 301
7.3 Adding the ImageView Component in XML
Code (4 of 4)
Figure 7-9 Images copied
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 302
7.4 Creating an Array for Images (1 of 2)
• The images for the GridView must be referenced
in the code and assigned to an array
• Arrays provide access to data by using a numeric
index (subscript) to identify each element
• Syntax:
Integer[] Animals =
{R.drawable.elephant,
R.drawable.gorilla,
R.drawable.leopard, R.drawable.panda,
R.drawable.polar, R.drawable.rhino};
Element of Array Image File
Animals[0] elephant.png
Animals[1] gorilla.png
Animals[2] leopard.png
Animals[3] panda.png
Animals[4] polar.png
Animals[5] rhino.png
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 303
7.4 Creating an Array for Images (2 of 2)
Figure 7-11 ImageView
referenced
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 304
7.5 Instantiating the GridView and ImageView
Components
• The code to instantiate the GridView assigns the name gvAnimals to the
component created in activity_main.xml
• Java code syntax for instantiating the GridView:
GridView grid = findViewById(R.id.gvAnimals);
• Java code syntax for instantiating the ImageView, which is assigned as a class
variable:
final ImageView pic = findViewById(R.id.ivLarge);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 305
7.6 Using a setAdapter with an ImageAdapter
(1 of 3)
• A setAdapter provides a data model for the GridView layout
− This adapter helps to set the data from an array list or a database to the
items of the grid
• Java code syntax to instantiate a custom BaseAdapter class called
ImageAdapter and apply it to the GridView using setAdapter():
grid.setAdapter(new ImageAdapter());
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 306
7.6 Using a setAdapter with an ImageAdapter
(2 of 3)
Figure 7-14 Instance of the
ImageAdapter class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 307
7.6 Using a setAdapter with an ImageAdapter
(3 of 3)
Figure 7-15 ImageAdapter
class and auto-generated
code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 308
Activity 7.1: Knowledge Check
1. What is the name of the component that displays a horizontal list of objects?
2. What is a View container?
3. What is each item in an array called?
4. Where should photos you use in an Android app project be stored?
5. The data model for a GridView layout is provided by a(n) _____.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 309
Activity 7.1: Knowledge Check Answers
1. What is the name of the component that displays a horizontal list of objects?
Answer: GridView component
2. What is a View container?
Answer: A rectangular area of the screen that displays an image or text object.
3. What is each item in an array called?
Answer: element
4. Where should photos you use in an Android app project be stored?
Answer: drawable folder
5. The data model for a GridView layout is provided by a(n) _____.
Answer: setAdapter
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 310
7.7 Coding the OnItemClickListener (1 of 4)
• The OnItemClickListener awaits user interaction within the GridView component
• The onItemClick() method is executed by the OnItemClickListener when the
user touches an image within the GridView component
− Has four arguments: parent, view, position, and id
• ListView and GridView components enable the Android device to monitor for
click events
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 311
7.7 Coding the OnItemClickListener (2 of 4)
• Java syntax for the onItemClick() method:
grid.setOnItemClickListener(new OnItemClickListener( ) {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
}};
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 312
7.7 Coding the OnItemClickListener (3 of 4)
Argument Purpose
AdapterView<?>
parent
The AdapterView records “where” the user actually touched the screen in the argument
variable parent. In other words, if the app has more than one View component, the
AdapterView determines if the user touched the GridView component or another
component in the application.
View view The View parameter is the specific View within the item that the user touched. This is the
View provided by the adapter.
int position This is one of the most important portions of this statement in the chapter project. The
position argument is an integer value that holds the position of the View in the adapter.
For example, if the user taps the elephant picture, the integer value of 1 is stored in
position because the elephant picture is the second image in the Animals array.
long id The GridView component is displayed across multiple rows of the Android device. The
argument id determines the row id of the item that was selected by the user. This is
especially useful for a GridView component that has multiple rows in the layout.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 313
7.7 Coding the OnItemClickListener (4 of 4)
Figure 7-16 GridView
OnItemClickListener
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 314
7.8 Coding a Custom Toast Notification
(1 of 2)
• Toast notifications display a temporary message to the user
• Toast notification code used in earlier chapters:
Toast.makeText(MainActivity.this, "Typical Toast Message",
Toast.LENGTH_SHORT).show();
• When you use a toast notification in an onItemClick() method rather than directly in the
MainActivity, the reference to MainActivity.this creates an error
• In Android programs, you can place the getBaseContext() method in another method
(such as onItemClick()) that is triggered only when the user touches the GridView
component
• If you do, the getBaseContext() method obtains a Context instance
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 315
7.8 Coding a Custom Toast Notification
(2 of 2)
• Figure 7-17 Toast message displayed when the user
selects the leopard image
• Notice that even though the leopard is in position
Animals[2] in the Animals array, the custom toast
message states “Selected Species 3”
− Array position 2 is really the third image because the
array values begin with 0
• Java syntax for this toast notification:
Toast.makeText(getBaseContext(),
"Selected Species " + (position + 1),
Toast.LENGTH_SHORT).show();
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 316
7.9 Displaying the setImageResource Image
(1 of 2)
• When the user selects an animal picture, a toast message appears with an
ImageView component that displays the selected image
• An ImageView component is defined either by the android:src attribute in the
XML element or by the setImageResource(int) method
• Java syntax for the setImageResource() method:
pic.setImageResource(Animals[position]);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 317
7.9 Displaying the setImageResource Image
(2 of 2)
• Customizing the ImageAdapter Class
− The ImageAdapter class was called with this line of code: gr.setAdapter(new
ImageAdapter (this));
− The ImageAdapter class determines the layout of the GridView
− The context and images of the GridView need to be referenced within the
ImageAdapter class
− The tasks to complete inside the ImageAdapter class:
 Manage the layout of the GridView
 Connect the data sources from the array for display within the GridView
component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 318
7.10 Defining the Context of the
ImageAdapter Class (1 of 3)
• A Context variable is used to load and access resources for the application
• Constructors are used to initialize the instance variables of an object
− A constructor is similar to a method but always has the same name as the
class whose instance members it initializes
− A constructor does not contain a return type
• When we call a constructor, we often have to pass a Context, and we often use
the Java command "this" to pass the activity Context to the application Context
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 319
7.10 Defining the Context of the
ImageAdapter Class (2 of 3)
• Java code that initializes the class variable named Context so it can hold each
image in the GridView temporarily before it is displayed:
private Context context;
public ImageAdapter(Context c){
context=c;
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 320
7.10 Defining the Context of the
ImageAdapter Class (3 of 3)
Figure 7-22 Passing the
Context to the ImageAdapter
Class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 321
7.11 Calculating the Length of an Array
• The length of an array is determined by the number of elements in the array
• When the ImageAdapter class is called, the getCount() method determines how many
pictures should be displayed in the GridView
− getCount() calls length(), which returns the number of pictures in the array:
public int getCount() {
return Animals.length;
}
− The Java length() method returns an integer value for any string or array
• A Java method is a series of statements that perform some repeated task
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 322
7.12 Coding the getView() Method (1 of 6)
• The getView() method uses a Context to create a new ImageView instance that
temporarily holds each image displayed in the GridView
• The returned pic is a scaled, resized image, ready to display in the GridView
component
• getView() method Java code:
public View getView(int position, View convertView, ViewGroup
parent){
pic = new ImageView(context);
pic.setImageResource(Animals[position]);
pic.setScaleType(ImageView.ScaleType.FIT_XY);
pic.setLayoutParams(new GridView.LayoutParams(330,300));
return pic;
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 323
7.12 Coding the getView() Method (2 of 6)
• Scaling keeps or changes the aspect ratio of the image within the ImageView
component
ScaleType Option Meaning
ImageView.ScaleType.CENTER This option centers the image within the View type but does not
change the aspect ratio (no scaling).
ImageView.ScaleType.CENTER_CROP This option centers the image within the View type and scales
the image uniformly, maintaining the same aspect ratio.
ImageView.ScaleType.FIT_XY This option scales the image to fit the View type. The aspect
ratio is changed to fit within the component.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 324
7.12 Coding the getView() Method (3 of 6)
• After the image is scaled, the GridView images are resized to fit the custom layout
• LayoutParams are set to GridView.LayoutParams(330,300)
− 330 pixels across the image
− Height of 300 pixels
• The last statement in the getView() method, return pic;, must return the instance
of the ImageView component named pic to display in the GridView component
• Running and Testing the Application
− Save the app, run it in the emulator, and test its functionality
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 325
7.12 Coding the getView() Method (4 of 6)
Figure 7-28 Complete code of
MainActivity.java (part 1)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 326
7.12 Coding the getView() Method (5 of 6)
Figure 7-28 Complete code of
MainActivity.java (part 2)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 327
7.12 Coding the getView() Method (6 of 6)
Figure 7-28 Complete code of
MainActivity.java (part 3)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 328
Activity 7.2: Knowledge Check
1. What do you use to initialize the instance variables of an object?
2. A series of Java statements that perform some repeated task is called a(n) _____.
3. What method does the OnItemClickListener execute when the user touches its
associated component?
4. What change to the Java code for a typical toast notification must you make to use a
toast message within an onItemClick() method?
5. What does the getView() method do?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 329
Activity 7.2: Knowledge Check Answers
(1 of 2)
1. What do you use to initialize the instance variables of an object?
Answer: constructor
2. A series of Java statements that perform some repeated task is called a(n)
_____.
Answer: method
3. What method does the OnItemClickListener execute when the user touches its
associated component?
Answer: onItemClick()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 330
Activity 7.2: Knowledge Check Answers
(2 of 2)
4. What change to the Java code for a typical toast notification must you make to
use a toast message within an onItemClick() method?
Answer: You must replace MainActivity.this with a Context class called
getBaseContext().
5. What does the getView() method do?
Answer: It uses a Context to create a new ImageView instance that
temporarily holds each image displayed in the GridView.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 331
Self-Assessment
1. How do you anticipate using the GridView component (or similar Android
layout tools) in your own apps? Did the use of this component in the
Endangered Species project give you any inspiration for your own app
projects?
2. Do you enjoy creating custom classes such as the ImageAdapter used in the
Endangered Species app? Or would you prefer to stick to classes provided in
the Android libraries? Explain the reasons for your preference.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 332
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 333
333
Android Bootcamp
for Developers
Using Java, 4e
Chapter 8: Design! Using a
DatePicker on a Tablet
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 334
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 08.01 Create an Android app on a tablet
• 08.02 Design a tablet app with modern design principles
• 08.03 Add an Android virtual device for the tablet
• 08.04 Design a tablet table layout
• 08.05 Add date, time, and clock components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 335
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 08.06 Create a Calendar class
• 08.07 Select the date from the DatePickerDialog
• 08.08 Add the onDateSet() method
• 08.09 Display the date using the getTime() method
• 08.10 Add an Android theme
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 336
Introduction
• Android Apps not limited to phones
• Tablets are used to:
− Connect to the Internet
− Play games
− Use social media
− Check e-mail
− And more
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 337
8.1 Creating an Android App on a Tablet
(1 of 2)
• Figure 8-2 DatePicker calendar component in a dialog box
• Figure 8-3 TextView component displays reservation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 338
8.1 Creating an Android App on a Tablet
(2 of 2)
• Steps required to create the Sailing Adventures application:
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 TextView components and the Button component
6. Initialize a DatePickerDialog with the current date and listen for the user to select
a date
7. Return the selected date
8. Display the selected reservation date in the TextView component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 339
8.2 Designing a Tablet App with Modern
Design Principles (1 of 2)
• 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)
• Native Android tablet apps create an optimal user experience based on the
most common tablet screen sizes
− Between 7.3 and 10.1 inches (diagonally)
− 2560 × 1600 pixel resolution and 16:10 screen ratio
− iPad Air specs: 9.7 inches, 2048 × 1536 pixels, 4:3 screen ratio
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 340
8.2 Designing a Tablet App with Modern
Design Principles (2 of 2)
• Design Best Practices for Tablets
− Understand the use case scenarios for tablets
− Keep screens uncluttered and use large-enough components
− Make the design simple and 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 resources for screen densities (DPI)
− Create a unique experience for phone and tablet designs
− Use larger fonts than with a phone app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 341
8.3 Adding an Android Virtual Device for the
Tablet (1 of 5)
• An API platform and Android virtual devices (ADVs) are necessary for
developing tablet apps
• The minimum required SDK should be set to API 13: Android 3.2 (Honeycomb)
to cover the oldest Android tablets
• The target SDK is automatically set to cover the most recent versions of Android
tablets
• Each Android device configuration is stored in an AVD
• First steps to create the Sailing Adventures app: create the new project and add
the appropriate AVD configuration
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 342
8.3 Adding an Android Virtual Device for the
Tablet (2 of 5)
Figure 8-5 New Project
window
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 343
8.3 Adding an Android Virtual Device for the
Tablet (3 of 5)
Figure 8-6 The
activity_main.xml file is
displayed to select a tablet
device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 344
8.3 Adding an Android Virtual Device for the
Tablet (4 of 5)
Figure 8-8 Selecting a Nexus
10 virtual device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 345
8.3 Adding an Android Virtual Device for the
Tablet (5 of 5)
• Creating the String Table
− The sailboat image for the Sailing Adventures app must be added to the
drawable folder
− The text values for the components must be entered into the strings.xml file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 346
8.4 Designing a Tablet Table Layout (1 of 7)
• The LinearLayout and TableLayout
elements can be combined to create
a simple, clean interface for a table
app
• TableLayout is composed of
TableRow components—one for each
row
• You add a view to a row to create
additional columns
sail.png Title
Day trip description
Reservation button
Reservation date after selection
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 347
8.4 Designing a Tablet Table Layout (2 of 7)
• The padding property can be used to offset the content of the component by a
specific number of pixels
• The typeface property sets the style of the text to a font family such as
monospace, sans serif, or serif
• The Sailing Adventures app's TableLayout includes four rows:
− Two stacked TextView components
− One Button component
− One more TextView component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 348
8.4 Designing a Tablet Table Layout (3 of 7)
• XML syntax for the app's structure:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView />
<TableLayout
<TableRow>
<TextView />
</TableRow>
<TableRow>
<TextView />
</TableRow>
<TableRow>
<Button />
</TableRow>
<TableRow>
<TextView />
</TableRow>
</TableLayout>
</LinearLayout>
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 349
8.4 Designing a Tablet Table Layout (4 of 7)
Figure 8-13 LinearLayout in
XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 350
8.4 Designing a Tablet Table Layout (5 of 7)
Figure 8-14 ImageView
component in XML code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 351
8.4 Designing a Tablet Table Layout (6 of 7)
Figure 8-15 TableLayout XML
code for first two TableRows
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 352
8.4 Designing a Tablet Table Layout (7 of 7)
Figure 8-16 TableLayout XML
code for last two TableRows
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 353
Activity 8.1: Knowledge Check
1. What is the difference between native applications and emulated applications?
2. What is the name of the first Android operating system that was designed to support
tablets?
3. What property is used to offset the content of a component by a specific number of
pixels?
4. How are rows and columns created in an app with a TableLayout?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 354
Activity 8.1: Knowledge Check Answers
1. What is the difference between native applications and emulated applications?
Answer: Native applications are installed locally on a platform whereas emulated
applications are converted in real time through a browser.
2. What is the name of the first Android operating system that was designed to support
tablets?
Answer: Android Honeycomb 3.2 (API 13)
3. What property is used to offset the content of a component by a specific number of
pixels?
Answer: padding
4. How are rows and columns created in an app with a TableLayout?
Answer: Rows are created by adding TableRow XML elements, and columns are
added by adding views to the TableRow elements.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 355
8.5 Using Date, Time, and Clock Components
(1 of 3)
• Android provides ready-to-use components called dialogs or dialog boxes that enable
the user to pick a valid time or date
• A DatePicker component is displayed in a dialog box to help the user determine and
choose a valid date
− Similar components: TimePicker, CalendarView, AnalogClock
• All Android devices keep a numeric representation of the system’s current date and
time, which can be displayed in multiple formats
• Creating a component to enter the date is crucial because requiring users to type the
date in a text box can lead to multiple errors, including incorrect
formats or typos
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 356
8.5 Using Date, Time, and Clock Components
(2 of 3)
• Instantiating the Components
− The tvReservation TextView component is referenced in multiple methods,
so this instantiation must be declared as a class variable
− The TextView component, Button component, and Button OnClickListener
must all be instantiated
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 357
8.5 Using Date, Time, and Clock Components
(3 of 3)
Figure 8-20 The onClickListener()
method for the button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 358
8.6 Creating a Calendar Class (1 of 4)
• The Android system date is accessed by using the Calendar class, which
converts information between a Date object and a set of integer fields such as
YEAR, MONTH, and DAY_OF_MONTH
• The getInstance() method returns a calendar date or time based on the system
settings
• The date constants in this class are YEAR, MONTH, and DAY_OF_MONTH
− DAY_OF_YEAR displays the day number of the current year
• Syntax for creating an instance of the Calendar class named c:
Calendar c = Calendar.getInstance();
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 359
8.6 Creating a Calendar Class (2 of 4)
• Date Format
− The DateFormat Java class formats the date into a String value
− Syntax: DateFormat fmtDate = DateFormat.getDateInstance();
• DatePickerDialog Input
− The DatePickerDialog allows you to select a date from a DatePicker View
 Other dialog boxes include the TimePickerDialog and ProgressDialog
− The field manipulation method called get accesses the system date or time
− set changes the current date or time
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 360
8.6 Creating a Calendar Class (3 of 4)
• DatePickerDialog Input (continued)
− Syntax for launching the DatePickerDialog in the onClick() method and
passing it the values for the current year, month, and day:
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(MainActivity.this, d,
c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
}
− The variable d is assigned later to the user-selected date
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 361
8.6 Creating a Calendar Class (4 of 4)
Figure 8-23 DatePickerDialog
launched within the onClick()
method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 362
8.7 Selecting the Date from the
DatePickerDialog
• To enable access to the system date, the variables are initialized and displayed
for the current YEAR, MONTH, and DAY_OF_MONTH
• Next, the DatePickerDialog component must await user interaction via an
OnDateSetListener named d, which listens for a callback indicating that the user
has filled in the reservation date
• Syntax for creating the onDateSetListener:
DatePickerDialog.OnDateSetListener d = new
DatePickerDialog.OnDateSetListener() {
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 363
Activity 8.2: Knowledge Check
1. What are some time and date components that are available in Java?
2. A(n) _____ listens for a callback indicating that the user has filled in a date in a
DatePickerDialog.
3. Calling _____ on a Calendar instance accesses the device system date’s month.
4. Why is using a component designed specifically to allow a user to enter a date preferable to
using a text box?
5. What class is responsible for converting information between a Date object and a set of
integer fields such as YEAR, MONTH, and DAY_OF_MONTH?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 364
Activity 8.2: Knowledge Check Answers
1. What are some time and date components that are available in Java?
Answer: TimePicker, DatePicker, CalendarView, Chronometer, and AnalogClock
2. A(n) _____ listens for a callback indicating that the user has filled in a date in a
DatePickerDialog.
Answer: OnDateSetListener
3. Calling _____ on a Calendar instance accesses the device system date’s month.
Answer: get(Calendar.MONTH)
4. Why is using a component designed specifically to allow a user to enter a date preferable to
using a text box?
Answer: A calendar component ensures the user’s entry is a valid date.
5. What class is responsible for converting information between a Date object and a set of
integer fields such as YEAR, MONTH, and DAY_OF_MONTH?
Answer: Calendar class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 365
8.8 Adding the onDateSet() Method
• When the user selects a date from the DatePickerDialog, the onDateSet()
method automatically obtains the date selected by the user
• Syntax for the onDateSet() method:
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 366
8.9 Displaying the Date Using the getTime()
Method (1 of 3)
• The getTime() method returns the time value in the Date object
• Syntax for displaying the user-selected date in the TextView object using the
getTime() method:
reservation.setText("Your reservation is set for " +
Date.format(c.getTime( )));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 367
8.9 Displaying the Date Using the getTime()
Method (2 of 3)
Figure 8-26 Completed Java
code for Sailing Adventures
app, Part 1
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 368
8.9 Displaying the Date Using the getTime()
Method (3 of 3)
Figure 8-26 Completed Java
code for Sailing Adventures
app, Part 2
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 369
8.10 Adding an Android Theme (1 of 4)
• A theme is a style applied to an Activity or an entire application
• Themes are Android’s mechanism for applying a consistent style to an app or
Activity on any device
• The style specifies the visual properties of the elements that make up a user
interface, such as color, height, padding, and font size
• Themes alter features such as the background wallpaper and may be used to
display (or not) a title bar or action bar
• Every UI element in the application should follow the app's parent theme
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 370
8.10 Adding an Android Theme (2 of 4)
Figure 8-28 Android theme
examples
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 371
8.10 Adding an Android Theme (3 of 4)
• Changing a Theme: You must define themes in the themes.xml or styles.xml file in the
values subfolder
• Running and Testing the Application: Save and test the application in the tablet
emulator
Theme Code in themes.xml Description
<style name="AppTheme" parent="Base.Theme.AppCompat"> Black background, gray title bar
<style name="AppTheme" parent="Base.Theme.AppCompat.Light"> White background, gray title bar
<style name="AppTheme" parent="Theme.Design.NoActionBar"> White background, no title bar
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 372
8.10 Adding an Android Theme (4 of 4)
Figure 8-29 The themes.xml
file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 373
Self-Assessment
1. Which of the best practices for tablet design listed in Chapter 8 do you think
are most important to ensuring a good user experience? Did any of these
guidelines surprise you?
2. How do you typically keep up to date with best practices for design in your
own software development? Consider how you might further improve your
skills in this area.
3. Have you used any of the Java classes/methods for handling dates and times
discussed in this chapter in other projects? If so, compare their application in
your project(s) with the way they are used in the Sailing Adventures app.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 374
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 375
375
Android Bootcamp
for Developers
Using Java, 4e
Chapter 9: Customize!
Navigating with a Primary/Detail
Flow Activity on a Tablet
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 376
Chapter Objectives
When you complete this chapter, you will be able to:
• 09.01 Describe responsive design for Android apps
• 09.02 Create an Android tablet project using an application template
• 09.03 Use the Primary/Detail Flow template
• 09.04 Modify the Primary/Detail Flow template
• 09.05 Design an XML TableLayout
• 09.06 Add a WebView component
• 09.07 Customize the content of the sample template file
• 09.08 Display a custom layout in the detail pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 377
9.1 Understanding Responsive Design
(1 of 5)
• Design elements such as fluid grids and flexible images can adapt to various
screen sizes
• The Bike and Barge application project relies on built-in templates that use the
best presentation mode based on the size of the device
− Multipane tablet interface
− Items from a list in the left pane can be tapped to make the selected content
display in the right pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 378
9.1 Understanding Responsive Design
(2 of 5)
Figure 9-2 Selecting Photos in
the left pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 379
9.1 Understanding Responsive Design
(3 of 5)
• Steps to create the Bike and Barge app:
− Create an application with a Primary/Detail Flow template
− Add the images to the drawable folder
− Add text to the String table
− Create the photos.xml file using a TableLayout for the detail pane of the first
list item
− Create the tour.xml file for the detail pane of the second list item
− Change the default TextView component to a WebView component
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 380
9.1 Understanding Responsive Design
(4 of 5)
• Steps to create the Bike and Barge app (continued):
− Update the AndroidManifest file to include an Internet permission
− Customize the PlaceholderContent class to display the item list
− Customize the PlaceholderContent class to connect the app to the website
− Modify the ItemDetailFragment.java class to:
 Display photos.xml in the detail pane
 Display tour.xml in the detail pane
 Display a website in a browser
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 381
9.1 Understanding Responsive Design
(5 of 5)
• Creating Apps with Responsive Design
− Responsive design: an approach to designing apps and websites that
provides an optimal viewing experience across as many devices as possible
− Android Studio provides responsive design templates, which allow you to
build an app once but display it on multiple devices
− Thanks to the use of one of these templates, the Bike and Barge app can be
run on a smartphone emulator without code changes
 There, the app runs as two Activities instead of two panes
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 382
9.2 Creating an Android Tablet Project Using
an Application Template (1 of 2)
• Application templates are used to create basic Android applications that can
immediately be run and tested on an Android device of any size
• Android templates are available when you create a new Android project
− The Bike and Barge app uses the Primary/Detail Flow application template
− A similar template is the Navigation Drawer Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 383
9.2 Creating an Android Tablet Project Using
an Application Template (2 of 2)
Figure 9-6 Android
application templates
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 384
9.3 Using the Primary/Detail Flow Template
(1 of 2)
• The Primary/Detail Flow template creates an adaptive, responsive layout for a
set of list items and associated detail content
− An interface design concept in which a list of items (the Primary list) appears
in a narrow vertical pane along the left edge of the screen
− The item details are displayed on the right side of the tablet screen in the
wider detail pane
− Referred to as a two-pane layout
• To begin creating the Bike and Barge app, you start a new project and select the
Primary/Detail Flow template
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 385
9.3 Using the Primary/Detail Flow Template
(2 of 2)
Figure 9-9 Creating the Bike
and Barge Android app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 386
9.4 Modifying the Structure of the
Primary/Detail Flow Template (1 of 3)
Figure 9-10 Template code
files in the java folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 387
9.4 Modifying the Structure of the
Primary/Detail Flow Template (2 of 3)
• Java Files
− ItemDetailFragment.java
 Displays the fragment_item_detail.xml layout file
 Can be customized to determine which detailed items to display
− ItemDetailHostActivity.java: displays the activity_item_detail.xml layout file if
a smartphone is detected
− ItemListFragment.java: displays the activity_item_list.xml layout file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 388
9.4 Modifying the Structure of the
Primary/Detail Flow Template (3 of 3)
• Layout Files
− activity_item_detail.xml: used by the app to display the FrameLayout
instance when a smartphone is detected
− fragment_item_detail.xml: used by the app when a smartphone is detected
to display the Primary list fragment
− fragment_item_detail.xml (sw600dp): used when a tablet is detected; the
app is displayed in a two-pane layout containing both the Primary item list
fragment and the item detail container
− item_list_content.xml: displays the detail pane using the onCreateView()
method when a smartphone or a tablet is detected
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 389
9.5 Designing an XML TableLayout (1 of 6)
• Before designing the XML layouts, first place the three required images in the drawable
folder and then reference them in the photos.xml layout
• The String table is responsible for the text displayed in the app
− The template's String table has two initial strings:
 The first string is displayed in the title bar of the tablet
 The second string is displayed in the title bar of a smaller device such as a
smartphone
− Other strings required for the app can be added to this table
• photos.xml uses a TableLayout consisting of three rows
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 390
9.5 Designing an XML TableLayout (2 of 6)
Figure 9-12 String table for
the Bike and Barge app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 391
9.5 Designing an XML TableLayout (3 of 6)
Figure 9-15 First row of the
TableLayout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 392
9.5 Designing an XML TableLayout (4 of 6)
Figure 9-16 Second and third
rows of the TableLayout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 393
9.5 Designing an XML TableLayout (5 of 6)
Figure 9-17 Completed
TableLayout in photos.xml in
Design view
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 394
9.5 Designing an XML TableLayout (6 of 6)
• Creating a TextView XML Layout for the Second List Item
− The second list item of the Bike and Barge app contains two TextView components
within a LinearLayout
− First TextView component XML code (the second is very similar):
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvTitle"
android:textSize="50sp"
android:paddingStart="50sp"
android:paddingBottom="60sp" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 395
9.6 Creating a WebView XML Layout (1 of 2)
• Webview object: a View component that displays webpages
• Permission: a restriction that limits access to a part of the code or to data on
the device
• Code syntax for requesting permission to connect to the Internet within the
AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"
/>
• Code syntax for enabling cleartext (disabled by default):
android:usesCleartextTraffic="true"
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 396
9.6 Creating a WebView XML Layout (2 of 2)
• The TextView component code must be modified to replace it with a WebView
component in the fragment_item_detail.xml layout file (sw600dp and regular
versions)
• WebView component syntax:
<WebView
android:id="@+id/item_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context= ".ItemDetailFragment" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 397
Activity 9.1: Knowledge Check
1. What does Android Studio do when you create a project using the
Primary/Detail Flow template?
2. What kind of template files are “activity_item_detail,” “fragment_item_detail,”
and “item_list_content”?
3. What code do you include in the AndroidManifest.xml file to give permission to
an app to access the Internet?
4. To provide an optimal viewing experience across as many devices as possible
for your Android application, you should implement _____ principles.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 398
Activity 9.1: Knowledge Check Answers
(1 of 2)
1. What does Android Studio do when you create a project using the
Primary/Detail Flow template?
Answer: Studio adds a group of Java and XML layout resource files, each
with a specific purpose, to the app project automatically.
2. What kind of template files are “activity_item_detail,” “fragment_item_detail,”
and “item_list_content”?
Answer: XML layout files
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 399
Activity 9.1: Knowledge Check Answers
(2 of 2)
3. What code do you include in the AndroidManifest.xml file to give permission to
an app to access the Internet?
Answer: <uses-permission android:name="android.permission.INTERNET" />
4. To provide an optimal viewing experience across as many devices as possible
for your Android application, you should implement _____ principles.
Answer: responsive design
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 400
9.7 Customizing the Content of the Sample
Template File
• The Java class file PlaceholderContent.java (in the placeholder folder) provides
sample content to display in the UI of the template
• This class can be customized or replaced as needed
• Code syntax for using the addItem command to display list items in the left pane
of the tablet display/first Activity on a smartphone:
// Add some sample items.
addItem(new PlaceholderItem("1", "Photos", ""));
addItem(new PlaceholderItem("2", "Tour", ""));
addItem(new PlaceholderItem("3", "Website",
"https://bikebarge.com"));
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 401
9.8 Displaying the Custom Layout in the
Detail Pane (1 of 6)
• A fragment is a piece of an application’s user interface or behavior that can be placed
in an Activity
− Fragments are essentially a sub-Activity hosted inside another Activity
• To determine if two String objects match exactly:
− Use the .equals() method, not the == operator
− The == operator compares if two objects are exactly the same object
− Two strings may be different objects but have the same exact characters
− The .equals() method is used to compare strings for equality
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 402
9.8 Displaying the Custom Layout in the
Detail Pane (2 of 6)
• Java syntax used to compare the value of a string:
if (mItem != null) {
if (mItem.id.equals("1"))
}
• Code within ItemDetailFragment.java to display a custom XML layout file named
photos.xml in the detail pane:
rootView = inflater.inflate(R.layout.photos, container, false);
− Three arguments: The first part displays the XML layout, the second part applies
the layout parameters to the container, and the third part is false, a Boolean type
declaring that the layout was already passed to the container
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 403
9.8 Displaying the Custom Layout in the
Detail Pane (3 of 6)
• Syntax used to display the webpage URL with the variable named item_url
within the WebView component:
((WebView)
rootView.findViewById(R.id.item_detail)).loadUrl(mItem.cont
ent);
• We want the page to load in the right pane, not in a separate screen in the
Chrome browser (the default), so we create an instance of the WebViewClient
class and assign it the shouldOverrideUrlLoading() callback method
• We code four conditional If statements to display the XML layout and the
webpage within the detail pane
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 404
9.8 Displaying the Custom Layout in the
Detail Pane (4 of 6)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 405
9.8 Displaying the Custom Layout in the
Detail Pane (5 of 6)
Figure 9-27
ItemDetailFragment.java class
customized for the third list
item
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 406
9.8 Displaying the Custom Layout in the
Detail Pane (6 of 6)
• Running and Testing the Application
− Click the Run app button on the Standard toolbar, select the Nexus 10
emulator to test the application, and then click the OK button
− Unlock the emulator (if necessary)
− Test each list item to ensure it works as intended
− You must have Internet connectivity to open the webpage and enough
memory available to handle the app's connection to the Web
− If you have trouble with the Nexus 10 emulator, the 10.1-inch WXGA tablet
emulator is an alternative
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 407
Activity 9.2: Knowledge Check
1. What is a fragment in the context of an Android app?
2. To determine if two String objects contain the same characters, you use the
_____; to determine if they are the same object, you use the _____.
3. I am a method with three arguments. The first part displays an XML layout, the
second part applies the layout parameters to the container, and the third part
is false, a Boolean type declaring that that layout was already passed to the
container. Who am I?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 408
Activity 9.2: Knowledge Check Answers
1. What is a fragment in the context of an Android app?
Answer: A piece of an application’s user interface or behavior that can be placed in
an Activity. A fragment is essentially a sub-Activity hosted inside another Activity.
2. To determine if two String objects contain the same characters, you use the _____; to
determine if they are the same object, you use the _____.
Answer: .equals() method; == operator
3. I am a method with three arguments. The first part displays an XML layout, the
second part applies the layout parameters to the container, and the third part is false,
a Boolean type declaring that that layout was already passed to the container. Who
am I?
Answer: inflate() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 409
Self-Assessment
1. You now have experience creating an app using an empty Activity and using a
template with placeholder content. Which of these methods do you find more
enjoyable overall? What aspects of each did you find most and least
ergonomic?
2. Which method do you expect to employ more often as a professional
developer? For which type of app projects do you anticipate using each route?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 410
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 411
411
Android Bootcamp
for Developers
Using Java, 4e
Chapter 10: Move! Creating
Animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 412
Chapter Objectives (1 of 2)
When you complete this chapter, you will be able to:
• 10.01 Create an Android application with Frame and Tween animation
• 10.02 Create frame-by-frame animation
• 10.03 Set the background resource
• 10.04 Use the start() and stop() methods
• 10.05 Move with Tween animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 413
Chapter Objectives (2 of 2)
When you complete this chapter, you will be able to:
• 10.06 Launch Tween animation
• 10.07 Add the layout for the Tween image
• 10.08 Set Tween animation attributes
• 10.09 Code the startAnimation() method
• 10.10 Change the orientation of the emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 414
10.1 Creating an Android Application with
Frame and Tween Animation (1 of 11)
• Animation is everywhere
− For example, in Dragon Ball Legends, Baldur's Gate, Word Link, Minecraft,
and Candy Crush
• A motion tween is used to animate an object and specifies
− A start state
− A uniform transition type
− The number of times to repeat the motion
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 415
10.1 Creating an Android Application with
Frame and Tween Animation (2 of 11)
Figure 10-1 Northern Lights
Animation app using Frame
animation
Figure 10-2 Northern Lights
Animation app using Tween
animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 416
10.1 Creating an Android Application with
Frame and Tween Animation (3 of 11)
• Steps to create the Northern Lights Animation app:
1. Add the layout for the image and button objects in activity_main.xml.
2. Add five images to the drawable folder.
3. Add a Frame animation XML file to the project.
4. Set the duration between frames in the frame-by-frame animation.
5. Declare and instantiate the ImageView, Button, and animationDrawable
controls.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 417
10.1 Creating an Android Application with
Frame and Tween Animation (4 of 11)
• Steps to create the Northern Lights Animation app (continued):
6. Code the OnClickListeners for the Button controls.
7. Run the Frame animation application.
8. Add a Tween animation XML file to rotate the last image.
9. Create a second Activity named Tween.java to launch the rotation Tween
animation with an XML layout.
10.When the application executes, change the orientation of the emulator.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 418
10.1 Creating an Android Application with
Frame and Tween Animation (5 of 11)
• Using Animation in Android
− Android provides two types of animation: Frame and Tween
− Frame animation (a.k.a. frame-by-frame animation) assigns a sequence of photos
to play with a predefined interval between images
 Like a slide show
 Images rapidly replaced by new, similar images
− Tween animation is created by a series of transformations on a single image
 Transformations can alter position, size, rotation, and transparency
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 419
10.1 Creating an Android Application with
Frame and Tween Animation (6 of 11)
• Adding the Layout for the Frame Image and Button Controls
− The layout specifications reside in a ConstraintLayout in activity_main.xml
− An ImageView control (ivLights) displays the Frame animation
− Two button controls (btStart and btStop) start and stop the animation,
respectively
− A Tween animation (a second Activity) is launched when the Frame
animation ends
− The "Help me choose" link can be referenced when selecting the minimum
SDK for a project
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 420
10.1 Creating an Android Application with
Frame and Tween Animation (7 of 11)
Figure 10-3 Setting up the
project in the New Project
window
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 421
10.1 Creating an Android Application with
Frame and Tween Animation (8 of 11)
Figure 10-4 String table
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 422
10.1 Creating an Android Application with
Frame and Tween Animation (9 of 11)
Figure 10-5 ImageView XML
code
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 423
10.1 Creating an Android Application with
Frame and Tween Animation (10 of 11)
Figure 10-6 Two Button
controls in the XML code
(Part 1)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 424
10.1 Creating an Android Application with
Frame and Tween Animation (11 of 11)
Figure 10-6 Two Button
controls in the XML code
(Part 2)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 425
10.2 Creating Frame-by-Frame Animation
(1 of 7)
• An animation-list root element is needed to reference images stored in the
drawable folders
− Each item in the animation-list code specifies how many milliseconds to
display the image
• The opening animation-list tag includes the android:oneshot attribute
− Set to true by default: the animation plays once and then stops and displays
the last frame
− When it is set to false, the animation plays repeatedly
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 426
10.2 Creating Frame-by-Frame Animation
(2 of 7)
• Code syntax for the animation-list element:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/lights1" android:duration= "100"/>
<item android:drawable="@drawable/lights2" android:duration= "100"/>
<item android:drawable="@drawable/lights3" android:duration= "100"/>
<item android:drawable="@drawable/lights4" android:duration= "100"/>
<item android:drawable="@drawable/lights5" android:duration= "100"/>
</animation-list>
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 427
10.2 Creating Frame-by-Frame Animation
(3 of 7)
• To create a Frame animation XML file, you:
− Add the image files to the drawable resources folder
− Add an XML file of the Animator resource type with animation-list as the root
element
− Nest an item element for each frame of the animation (image) within the
animation-list element
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 428
10.2 Creating Frame-by-Frame Animation
(4 of 7)
Figure 10-8 Creating the
animation.xml file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 429
10.2 Creating Frame-by-Frame Animation
(5 of 7)
Figure 10-10
Entering the
animation-list
items with image
names and
duration
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 430
10.2 Creating Frame-by-Frame Animation
(6 of 7)
• Coding the AnimationDrawable Object
− The AnimationDrawable class provides the methods for drawable
animations to create a sequence of frame-by-frame images played in order
− In Android development, frame-based animations and image transitions are
defined as drawables
− The instance of AnimationDrawable is instantiated as a class variable
because it is used in multiple methods within the MainActivity class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 431
10.2 Creating Frame-by-Frame Animation
(7 of 7)
Figure 10-11
Instantiating the
AnimationDrawable
class variable
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 432
10.3 Setting the Background Resource
(1 of 6)
• The Background property of an image can be set to any full drawable resource, such
as a .png file, a 9-patch image file, or a solid color
• A 9-patch image has predefined “stretching” areas that maintain the same look on
different screen sizes
− Named for the nine areas, called patches, that scale separately
• The setBackgroundResource() method places images in a frame-by-frame display
that resembles a slide show
− Each frame points to one of the images that were assembled in the XML resource
file
− The ImageView instance is the image that you want to animate; the image is set to
the animation drawable as its background
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 433
10.3 Setting the Background Resource
(2 of 6)
• Syntax for setting the background resource:
ImageView ivFrame=findViewById(R.id.ivLights);
ivFrame.setBackgroundResource(R.drawable.animation);
lightsAnimation=(AnimationDrawable)
ivFrame.getBackground();
− The first line instantiates the ImageView control
− The second line sets the animation-list code within animation.xml as the
Background property of the ivFrame ImageView
− The third line assigns the instance of AnimationDrawable called
lightsAnimation as the background of the five images to display in the
animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 434
10.3 Setting the Background Resource
(3 of 6)
Figure 10-14 The
getBackground() method
prepares the
AnimationDrawable object
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 435
10.3 Setting the Background Resource
(4 of 6)
• Adding Two Button Controls
− The Button controls in the Northern Lights Animation app turn the Frame
animation on and off
− Both use a setOnClickListener to await user interaction
− For the app project, you must write code to instantiate each Button control
and then to add the setOnClickListener to each
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 436
10.3 Setting the Background Resource
(5 of 6)
Figure 10-16 Coding the
button to start the Tween
animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 437
10.3 Setting the Background Resource
(6 of 6)
Figure 10-18 OnClickListener
for the second button
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 438
Activity 10.1: Knowledge Check
1. How does Tween animation work?
2. What is an animation-list?
3. What kind of image has predefined “stretching” areas that maintain the same
look on different screen sizes?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 439
Activity 10.1: Knowledge Check Answers
1. How does Tween animation work?
Answer: Tween animation creates animation by performing a series of
transformations on a single image such as changing its position, size, rotation,
and transparency.
2. What is an animation-list?
Answer: An element used to list, in sequence, the images that will be
displayed in an animation.
3. What kind of image has predefined “stretching” areas that maintain the same
look on different screen sizes?
Answer: 9-patch image
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 440
10.4 Using the start() and stop() Methods
(1 of 2)
• After associating AnimationDrawable with the animation images and coding the
buttons, you can use the start() and stop() methods of the drawable objects to
control the Frame animation
− Syntax:
lightsAnimation.start();
lightsAnimation.stop();
• The start() method begins the Frame animation to run continuously because
oneshot is set to false
• The stop() method thus must be called to stop the animation when the button
that begins the Tween animation for the app is tapped
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 441
10.4 Using the start() and stop() Methods
(2 of 2)
Figure 10-20 Entering the
stop() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 442
10.5 Moving with Tween Animation
• Tween effects are transitions that change objects from one state to another
− Alpha: transitions an object from one level of transparency to another, where
0.0 is transparent and 1.0 is opaque
− Rotate: spins an object from one angular position to another
− Scale: transitions the size of an object (grow or shrink) on an X/Y scale
− Translate: moves the object vertically or horizontally by a percentage relative
to the element width
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 443
10.6 Launching Tween Animation (1 of 3)
• The "Start Tween Animation" button in the Northern Lights Animation app
triggers two actions within the second onClick() method
− The stop() method concludes the Frame animation
− A startActivity intent launches a second Activity named Tween.java
• A new Java class and a second XML layout file must be created for the new
Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 444
10.6 Launching Tween Animation (2 of 3)
Figure 10-22 Creating the
Tween.java class and
activity_tween layout
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 445
10.6 Launching Tween Animation (3 of 3)
Figure 10-23 The startActivity
launches the Tween class
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 446
10.7 Adding the Layout for the Tween Image
(1 of 2)
• The second Activity, Tween.java, defines a second layout named
activity_tween.xml
• This XML layout contains a single ImageView control called ivTween
− It references the fifth image named lights5 that will be rotated with the Tween
animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 447
10.7 Adding the Layout for the Tween Image
(2 of 2)
Figure 10-24 ImageView
control coded in
activity_tween.xml
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 448
10.8 Setting Tween Animation Attributes
(1 of 4)
• Android uses an XML file-creator utility that supports 10 different resource types
that can be set as attributes
− The default type is Layout
− The Tween animation resource type can be used to create an XML file for a
Tween animation
− You use this type with rotate as the root element to code a rotation
• Attributes set within the rotate element's opening tag are used to define the
Tween animation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 449
10.8 Setting Tween Animation Attributes
(2 of 4)
• Rotation attributes:
− android:fromDegrees="0" and android:toDegrees="359" spin the
object from 0 to 359 degrees (360 degrees total)
− android:pivotX="50%" and android:pivotY="50%" set the pivot
point for the rotation at the center of the object
− android:duration="2000" sets the duration for each rotation to 2,000
milliseconds
− android:repeatCount="5" will rotate the object six times (it sets the
number of rotations after the initial rotation)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 450
10.8 Setting Tween Animation Attributes
(3 of 4)
Figure 10-25 New Resource
File dialog box
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 451
10.8 Setting Tween Animation Attributes
(4 of 4)
Figure 10-26 The rotate
attributes in rotation.xml
within the anim folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 452
10.9 Coding the startAnimation() Method
(1 of 2)
• The StartAnimation() method begins animating a View object by calling the
AnimationUtils class utilities to access the resources necessary to load the
animation
• A Tween animation can perform a series of simple transformations with
startAnimation(), changing position, size, rotation, and/or transparency on the
contents of a View object
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 453
10.9 Coding the startAnimation() Method
(2 of 2)
Figure 10-28 Image rotates
using Tween animation
(completed code for
Tween.java)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 454
Activity 10.2: Knowledge Check
1. Which tween effect makes an object appear to grow or shrink?
2. What files were added to the Northern Lights Animation app to create the
image rotation effect?
3. Which rotate attribute or attributes are used to set the point around which the
object rotates?
4. How can you apply a Tween animation to a View object in order to launch the
tween effect?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 455
Activity 10.2: Knowledge Check Answers
(1 of 2)
1. Which tween effect makes an object appear to grow or shrink?
Answer: scale
2. What files were added to the Northern Lights Animation app to create the
image rotation effect?
Answer: Tween.java and activity_tween.xml
3. Which rotate attribute or attributes are used to set the point around which the
object rotates?
Answer: pivotX and pivotY
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 456
Activity 10.2: Knowledge Check Answers
(2 of 2)
4. How can you apply a Tween animation to a View object in order to launch the
tween effect?
Answer: Call the startAnimation() method on the View object
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 457
10.10 Changing the Emulator to Landscape
Orientation
• The default screen orientation layout for the emulator is vertical
• Shortcuts for switching to a landscape orientation:
− On a PC: Fn + left Ctrl + F12 or 7 on the keypad with Num Lock off
− On a Mac: Fn + Ctrl + F12
• Running and Testing the Application
− Click the Run app button
− Test the two buttons' functionality
− Practice changing the orientation of the emulator
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 458
Self-Assessment
1. What is your favorite app that features animations prominently? How are they
utilized in this app, and how do they contribute to your experience as a user?
2. Think about the categories of apps you would like to develop as a
professional. To what extent, and in what capacity, would they incorporate
animations? Which of the two types of Android animations you learned about
in Chapter 10 do you expect these effects would be coded as?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 459
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 460
460
Android Bootcamp
for Developers
Using Java, 4e
Chapter 11: Discover! Persistent
Data
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 461
Chapter Objectives
When you complete this chapter, you will be able to:
• 11.01 Create an Android project using persistent data
• 11.02 Use different types of shared preferences
• 11.03 Use internal and external storage
• 11.04 Save to a database connection
• 11.05 Save data using a network connection
• 11.06 Write persistent data using a SharedPreferences object
• 11.07 Retrieve data using the getString() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 462
11.1 Creating an Android Project Using
Persistent Data (1 of 4)
Figure 11-1 Electric Car
Financing app
Figure 11-2 Car payment
computed using persistent
data in second Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 463
11.1 Creating an Android Project Using
Persistent Data (2 of 4)
• Steps required to create the Electric Car Financing app:
1. Add strings to the String table.
2. Add images to the drawable folder.
3. Design two XML layouts for the first and second Activity.
4. Instantiate the XML components in the first Activity.
5. Establish a SharedPreferences object to store the data entered.
6. Write data to the SharedPreferences object.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 464
11.1 Creating an Android Project Using
Persistent Data (3 of 4)
• Steps required to create the Electric Car Financing app (continued):
7. Launch a second Activity.
8. Initialize the XML components on the second Activity.
9. Retrieve the data from the SharedPreferences object.
10.Calculate the monthly payment for the car loan and display the appropriate
image for the number of loan years using an If structure.
11.Display the monthly payment on the second Activity.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 465
11.1 Creating an Android Project Using
Persistent Data (4 of 4)
• Persistent data stores values permanently by placing the information in a file
• Persistent data can be stored in five ways in Android applications:
− Shared preferences, which stores private data in key-value pairs
− Internal storage, which stores private data in the memory of the device
− External storage, which stores data that can be available to other apps on shared
external storage
− SQLite database, which stores structured data in a private database
− Network connection, which stores data on a web server
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 466
11.2 Using Different Types of Shared
Preferences
• The SharedPreferences object can be used to save any primitive data: Booleans,
floats, ints, longs, and strings
• Shared preferences are best when your app needs to save small chunks of data in a
name/value pair
• Steps necessary to save persistent data in a SharedPreferences file:
− Obtain an instance of the SharedPreferences file
− Create a SharedPreferences.Editor object
− Assign values to SharedPreferences objects using the putString() method
− Save the values to the preferences file using the commit() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 467
11.3 Using Internal and External Storage
(1 of 2)
• Internal storage means storing the information directly on the device’s internal
drive
− Saved files on the device are available only to the app that created the files
− Be careful: insufficient internal storage space can drastically affect the speed
of an Android device and its battery life
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 468
11.3 Using Internal and External Storage
(2 of 2)
• Android apps can save persistent data to external storage, e.g., the device’s SD
(Secure Digital) card
• All applications can read and write files placed on the external storage and the
Android smartphone or tablet owner can remove them
• To use external storage, the following permissions are necessary in the Android
Manifest file:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 469
11.4 Saving to a Database Connection
• A database is the perfect choice for storing a large amount of data
• SQLite is a lightweight, preloaded mobile database engine
• SQLite has been available since the Cupcake 1.5 version of Android
• It occupies a small amount of disk memory
• Android apps model data items in tables and columns, with optional
relationships between the entities within the database
• The tables can be queried using SQL statements (SQL stands for Structured
Query Language)
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 470
Activity 11.1: Knowledge Check
1. What are the five ways to store persistent data in an Android application?
2. What step must you take before you can use external storage to save Android
app data?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 471
Activity 11.1: Knowledge Check Answers
1. What are the five ways to store persistent data in an Android application?
Answer: SharedPreferences object (stores private data in key-value pairs)
Internal storage (stores private data in the memory of the device)
External storage (stores data that can be available to other apps on shared external
storage)
SQLite database (stores structured data in a private database)
Network connection (stores data on a web server)
2. What step must you take before you can use external storage to save Android app
data?
Answer: Add the WRITE_EXTERNAL_STORAGE and
READ_EXTERNAL_STORAGE permissions to the Android Manifest file.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 472
11.5 Saving Data Using a Network
Connection (1 of 20)
• If connected to the Internet (a 4G/5G or Wi-Fi connection), persistent data can
be stored and retrieved using a web service
• Before an app attempts to connect to a network, it should check to see whether
an Internet connection is available
• If a connection is not available, the user cannot save or retrieve the persistent
data
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 473
11.5 Saving Data Using a Network
Connection (2 of 20)
• Creating XML Layout Files
− First, create the Electric Car Financing project with API 23: Android 6.0
(Marshmallow) as the minimum SDK
− Load the images into the drawable folder
− Enter the strings for display into the String table
− Drag into and position components within the emulator: a TextView, three
EditText Number components, a Button, and an ImageView
− Modify the XML code to customize the components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 474
11.5 Saving Data Using a Network
Connection (3 of 20)
Figure 11-3 Setting up the
Electric Car Financing project
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 475
11.5 Saving Data Using a Network
Connection (4 of 20)
Figure 11-4 Image files in the
drawable folder
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 476
11.5 Saving Data Using a Network
Connection (5 of 20)
Figure 11-5 String values for
the app
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 477
11.5 Saving Data Using a Network
Connection (6 of 20)
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/tvTitle"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="@+id/ivOpening"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.067" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 478
11.5 Saving Data Using a Network
Connection (7 of 20)
<EditText
android:id="@+id/tvYears"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/hint1"
android:inputType="number"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.423"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.208" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 479
11.5 Saving Data Using a Network
Connection (8 of 20)
<EditText
android:id="@+id/tvLoan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvYears"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/hint2"
android:inputType="number"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="@+id/ivOpening"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.423"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.334" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 480
11.5 Saving Data Using a Network
Connection (9 of 20)
<EditText
android:id="@+id/tvInterest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvLoan"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/hint3"
android:inputType="numberDecimal"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.423"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.463" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 481
11.5 Saving Data Using a Network
Connection (10 of 20)
<Button
android:id="@+id/btPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvInterest"
android:layout_centerHorizontal="true"
android:layout_marginBottom="276dp"
android:backgroundTint="#000000"
android:text="@string/btPayment"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 482
11.5 Saving Data Using a Network
Connection (11 of 20)
<ImageView
android:id="@+id/ivOpening"
android:layout_width="346dp"
android:layout_height="242dp"
android:layout_below="@+id/btPayment"
android:layout_centerHorizontal="true"
android:contentDescription="@string/ivDescription"
android:src="@drawable/opening"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.441"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 483
11.5 Saving Data Using a Network
Connection (12 of 20)
• Creating a Second Activity and XML Layout
− Create a second Activity, which consists of a second class and a second
XML layout file
− Drag into and position within the emulator the UI components: two TextView
components and one ImageView component
− Modify the layout XML code to customize the components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 484
11.5 Saving Data Using a Network
Connection (13 of 20)
Figure 11-7 Creating a second
class named Payment and a
second XML layout named
activity_payment
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 485
11.5 Saving Data Using a Network
Connection (14 of 20)
<TextView
android:id="@+id/tvTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/tvTitle"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.101" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 486
11.5 Saving Data Using a Network
Connection (15 of 20)
<TextView
android:id="@+id/tvMonthlyPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.27" />
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 487
11.5 Saving Data Using a Network
Connection (16 of 20)
<ImageView
android:id="@+id/ivYears"
android:layout_width="396dp"
android:layout_height="285dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/ivDescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.935" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 488
11.5 Saving Data Using a Network
Connection (17 of 20)
• Instantiating the XML Components
− Instantiate the EditText components from the first Activity
− Instantiate the Button component from the first Activity, creating an
OnClickListener stub
− Initialize the variables that will be used for calculations to values parsed from
the user's entries in the EditText components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 489
11.5 Saving Data Using a Network
Connection (18 of 20)
Figure 11-9 EditText
components instantiated
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 490
11.5 Saving Data Using a Network
Connection (19 of 20)
Figure 11-10 Button
OnClickListener stub
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 491
11.5 Saving Data Using a Network
Connection (20 of 20)
Figure 11-11 Variables
intYears, intLoan, and
decInterest assigned
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 492
11.6 Writing Persistent Data with
SharedPreferences (1 of 5)
• Data is saved to an XML file as a key-value pair
− The key is a string such as “key1” that uniquely identifies the preference
− The value is the data represented as a string, int, long, float, or Boolean
• Data types supported by the SharedPreferences class:
− putString()—stores string values
− putInt()—stores integer values
− putLong()—stores long values
− putFloat()—stores float values
− putBoolean()—stores Boolean values
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 493
11.6 Writing Persistent Data with
SharedPreferences (2 of 5)
• Java syntax for storing data with the SharedPreferences object:
final SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("key1", intYears);
editor.putInt("key2", intLoan);
editor.putFloat("key3", decInterest);
editor.commit();
• Within the editor, you can also remove a single preference pair using the
remove() method or remove all preferences using the clear() method
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 494
11.6 Writing Persistent Data with
SharedPreferences (3 of 5)
Figure 11-13 Three values
(persistent data) written in an
XML data file
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 495
11.6 Writing Persistent Data with
SharedPreferences (4 of 5)
• Launching the Second Activity
− Add the statement startActivity(new
Intent(MainActivity.this, Payment.class)); to the onClick()
method
• Instantiating the Second Activity Components
− Instantiate the TextView and ImageView components
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 496
11.6 Writing Persistent Data with
SharedPreferences (5 of 5)
Figure 11-15 Instantiating the
TextView and ImageView
components in Payment.java
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 497
11.7 Retrieving Data Using getString()
Methods (1 of 7)
• You do not need an editor to read saved data in SharedPreferences
• Retrieve the SharedPreferences object and use the appropriate method to
retrieve a key’s value by name
− getString()—retrieves string values
− getInt()—retrieves integer values
− getLong()—retrieves long values
− getFloat()—retrieves float values
− getBoolean()—retrieves Boolean values
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 498
11.7 Retrieving Data Using getString()
Methods (2 of 7)
• Each "get" method has two parameters: the preference key string and a default
value to return if the preference is undefined
• Java syntax for creating a SharedPreferences instance and loading data from
the persistent data XML file:
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
int intYears = sharedPref.getInt("key1", 0);
int intLoan = sharedPref.getInt("key2", 0);
float decInterest = sharedPref.getFloat("key3", 0);
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 499
11.7 Retrieving Data Using getString()
Methods (3 of 7)
Figure 11-17
SharedPreferences values are
retrieved
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 500
11.7 Retrieving Data Using getString()
Methods (4 of 7)
• Displaying the Correct ImageView Component
− An ImageView control can display an image by assigning a source path
(android:src="drawable/filename") in the XML layout file
− Alternatively, it can display an image by dynamically assigning the image
within the Java code:
image.setImageResource(R.drawable.three);
− For the Electric Car Financing app, a monthly payment is calculated and
formatted for display
− A nested If decision structure is used to select an image to display
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 501
11.7 Retrieving Data Using getString()
Methods (5 of 7)
Figure 11-19 The monthly
payment is displayed in the
second Activity
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 502
11.7 Retrieving Data Using getString()
Methods (6 of 7)
Figure 11-21 Closing Else
statement
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 503
11.7 Retrieving Data Using getString()
Methods (7 of 7)
• Running and Testing the Application
− Run the app in the emulator
− The first Activity saves data you enter to an XML file using the
SharedPreferences object
− The second Activity launches the Payment.java file, which retrieves the
stored data and calculates the monthly payment for the car loan
− The monthly payment is shown, and a decision structure determines which
image should be displayed to indicate the loan term
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 504
Activity 11.2: Knowledge Check
1. What is the difference between a key and a value in a SharedPreferences
XML file?
2. What two SharedPreferences class methods would you call to store an integer
value?
3. What methods of the SharedPreferences object are used to retrieve Android
data?
4. What Java method can you use to dynamically assign an image to display
within an ImageView component?
5. Answer: setImageResource()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 505
Activity 11.2: Knowledge Check Answers
(1 of 2)
1. What is the difference between a key and a value in a SharedPreferences
XML file?
Answer: The key is a string such as “key1” that uniquely identifies the
preference, and the value is the data represented as a string, int, long, float, or
Boolean value.
2. What two SharedPreferences class methods would you call to store an integer
value?
Answer: putInt() and commit()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 506
Activity 11.2: Knowledge Check Answers
(2 of 2)
3. What methods of the SharedPreferences object are used to retrieve Android
data?
Answer: getString() retrieves string values
getInt() retrieves integer values
getLong() retrieves long values
getFloat() retrieves float values
getBoolean() retrieves Boolean values
4. What Java method can you use to dynamically assign an image to display
within an ImageView component?
Answer: setImageResource()
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 507
Self-Assessment
1. Think about the kinds of Android applications you anticipate working on as a
developer. What kinds of data, and what quantities of data, would these apps
need to store and retrieve to function usefully?
2. Which of the methods for storing persistent data that you learned about in
Chapter 11 do you think should be used for the apps you envisioned?
3. Consider the personal data that many apps store about individuals. How much
do you know about the coding practices and other procedures used to
safeguard personal data? What else do you need to learn to write apps that
offer reasonable data protection?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 508
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 509
509
Android Bootcamp
for Developers
Using Java, 4e
Chapter 12: Finale! Publishing
Your Android App
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 510
Chapter Objectives
When you complete this chapter, you will be able to:
• 12.01 Describe Google Play
• 12.02 Target various device configurations and languages
• 12.03 Test your app before publishing
• 12.04 Create an Android App Bundle
• 12.05 Prepare promotional materials to upload to Google Play
• 12.06 Publish your app on Google Play
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 511
12.1 Using Google Play (1 of 3)
• There are many distribution networks
• Apps can be published to:
− Google Play (Google’s app store, the largest marketplace)
− Amazon Appstore
− Huawei AppGallery
− Xiaomi Mi GetApps
− Aurora
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 512
12.1 Using Google Play (2 of 3)
• Steps to publish an app:
1. Test the app
2. Prepare the app for publication
3. Create an AAB package and digitally sign the application
4. Prepare promotional materials
5. Publish the app to Google Play
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 513
12.1 Using Google Play (3 of 3)
• Google Play is a digital repository that serves as the online storefront for paid
and free Android apps
− URL: https://play.google.com
− Contains the features and services of Android apps, Google Music, and
Google e-books
− Provides free cloud storage services
− Used by over 190 countries
− Apps selected on Google Play install directly to the Android device
− Part of the default setup on new Android devices
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 514
12.2 Targeting Device Configurations and
Languages (1 of 4)
• You can reach a larger audience by translating your app into multiple languages
• To support multiple languages:
− Create a resource directory for each language in the strings directory (the
strings.xml file) using the Translations Editor
− One application then recognizes multiple languages
• You can use the Google Translate service (https://translate.google.com) to
translate into over 100 different languages
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 515
12.2 Targeting Device Configurations and
Languages (2 of 4)
• Adding Localization Using the Translations Editor
− The Android Studio Translations Editor supports multiple languages
− Localization: the process of adapting an app or web page to a language
while considering cultural differences
 Differences include whether the language is read left-to-right or right-to-
left, calendar layout, measurements, currency, date and time formats,
and traditions of the target audience
− The globe-shaped Add Locale icon in the Translations Editor provides
access to a list of locales around the world
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 516
12.2 Targeting Device Configurations and
Languages (3 of 4)
Figure 12-2 Locale listing in
the Translations Editor
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 517
12.2 Targeting Device Configurations and
Languages (4 of 4)
Figure 12-4 Red text
on key designates
missing translation
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 518
12.3 Testing Your App before Publication
• Use the Android Studio built-in emulators to test an app's design and
functionality
• Check real-world functionality by using the Android Debug Bridge (ADB),
which allows Android Studio to communicate with a connected Android device
− Access the Settings > Developer options on the device and check the USB
debugging option
− Install a USB driver for ADB on the computer (Windows only)
− Run the app in Studio and select the connected device
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 519
12.4 Creating an Android App Bundle (1 of 5)
• The required app package format for Google Play is Android App Bundle
(AAB) format
• An .aab file is similar to a .zip file that contains the application, the manifest file,
and all associated resources, such as image files, music, and other required
content
• You use the Android Studio Build menu to build a release-ready .aab file signed
with your private key and optimized for publication
− All Android apps must be digitally signed with a certificate that identifies the
author and establishes trust relationships between applications
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 520
12.4 Creating an Android App Bundle (2 of 5)
Figure 12-6 Using the Build
menu in Android Studio
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 521
12.4 Creating an Android App Bundle (3 of 5)
Figure 12-7 Generate Signed
Bundle or APK dialog box
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 522
12.4 Creating an Android App Bundle (4 of 5)
Figure 12-8 Creating a key in
the Generate Signed Bundle
or APK dialog box
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 523
12.4 Creating an Android App Bundle (5 of 5)
Figure 12-9 Selecting the
debug option
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 524
Activity 12.1: Knowledge Check
1. Which tool is used to communicate with a connected Android device through Android
Studio?
2. When you adapt an app or web page to a language while considering cultural
differences such as the direction in which the language is read, date and time
formats, units of measurement, and currency, you are adding _____.
3. To publish an app on Google Play, you must package it in _____ format, which has
the _____ file extension.
4. What is used to sign your Android application with information from your local
system?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 525
Activity 12.1: Knowledge Check Answers
1. Which tool is used to communicate with a connected Android device through Android Studio?
Answer: Android Debug Bridge (ADB)
2. When you adapt an app or web page to a language while considering cultural differences such
as the direction in which the language is read, date and time formats, units of measurement,
and currency, you are adding _____.
Answer: localization
3. To publish an app on Google Play, you must package it in _____ format, which has the _____
file extension.
Answer: Android App Bundle (AAB), .aab
4. What is used to sign your Android application with information from your local system?
Answer: A private key or certificate
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 526
12.5 Preparing Promotional Materials to
Upload (1 of 11)
Figure 12-10 National
Geographic Android app from
Google Play
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 527
12.5 Preparing Promotional Materials to
Upload (2 of 11)
• Providing Images
− Application icon (required): 512 x 512 pixels in size, stored in a 32-bit .png
file
− Screenshots (2 required; up to 8): 480 x 320, 800 x 480, or 854 x 480 pixels
in size; PNG files
− Video that demonstrates the app (optional): should highlight the top features
and be between 30 seconds and 2 minutes
 Can link to the video on YouTube
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 528
12.5 Preparing Promotional Materials to
Upload (3 of 11)
• Providing a Description
− An app description provides a quick overview to the purpose of the app and
what it does
− Include features the app provides
− You want to sell the app to the widest audience possible
− Motivate users to download the app
− Revise the description as you update the app
− Utilize quotes from positive reviews when possible
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 529
12.5 Preparing Promotional Materials to
Upload (4 of 11)
Figure 12-12 National
Geographic reviews
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 530
12.5 Preparing Promotional Materials to
Upload (5 of 11)
• Providing a Description (continued)
− Pricing an app
 Users may try a free app, then buy additional features, including in-app
purchases
 Some users will never pay for an average app, but many will pay for a
great one
− When you upload an app to Google Play, you select one or more of the
application categories to classify it
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 531
12.5 Preparing Promotional Materials to
Upload (6 of 11)
Category Example Types of Apps
Books & Reference Book readers, reference books, textbooks, dictionaries, thesauruses, wikis
Business Document editors/readers, package tracking, remote desktops, email management, job
searches
Comics Comic players, comic titles
Communications Messaging, chat/IM, dialers, address books, browsers, call management
Education Exam preparation, study aids, vocabulary, educational games, language learning
Finance Banking, payment, ATM finders, financial news, insurance, taxes, portfolio/trading, tip
calculators
Health & Fitness Personal fitness, workout tracking, diet and nutritional tips, health and safety
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 532
12.5 Preparing Promotional Materials to
Upload (7 of 11)
Category Example Types of Apps
Libraries & Demo Software libraries
Lifestyle Recipes, style guides
Media & Video Subscription movie services, remote controls, media/video players
Medical Drug and clinical references, calculators, handbooks for healthcare providers, medical
journals and news
Music & Audio Music services, radios, music players
News & Magazines Newspapers, news aggregators, magazines, blogging
Personalization Wallpapers, live wallpapers, home screens, lock screens, ring tones
Photography Cameras, photo-editing tools, photo management and sharing
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 533
12.5 Preparing Promotional Materials to
Upload (8 of 11)
Category Example Types of Apps
Productivity Notepads, to-do lists, keyboards, printing, calendars, backup, calculators, conversion
Shopping Online shopping, auctions, coupons, price comparison, grocery lists, product reviews
Social Social networking, check-in, blogging
Sports Sports news and commentary, score tracking, fantasy team management, game
coverage
Tools System utility tools
Travel & Local City guides, local business information, trip management tools
Weather Weather reports
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 534
12.5 Preparing Promotional Materials to
Upload (9 of 11)
• Including Contact and Social Networks
− A social networking presence for your app enables you to build relationships
with customers and communicate with your fans
• Setting Versions for your App
− After an app is published, you can update it with new features and new SDK
platforms
− Allows the Android OS to check whether a newer version of the app is
available from the Google Play Store and then prompt the user to upgrade
the app, if necessary
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 535
12.5 Preparing Promotional Materials to
Upload (10 of 11)
• Setting Versions for your App (continued)
− Syntax to set the version of code and version name number within the
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/androi
d"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="1"
android:versionName="1.0">
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 536
12.5 Preparing Promotional Materials to
Upload (11 of 11)
• Registering for a Google Play Account
− A Google account is needed
− Register at https://play.google.com/apps/publish
− A one-time payment of $25 is required to register as an Android application
developer and enrolls you in a Google Merchant account
− If you charge for apps, you also obtain a Google Wallet account and Google
Merchant disperses revenue for application sales
 For the first $1 million of revenue, you get 85% and the phone carriers get 15%
 For profits >$1 million, the service fee increases to 30%
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 537
12.6 Publishing Your App to Google Play
(1 of 4)
• Once you create an account, the Developer Console pages take you through
the steps to upload your unlocked app .aab file and the promotional assets
− Maximum size for the .aab file: 150 MB
• Ensure your app complies with Google's content policies and with local laws
• After launching your app, you can visit your Dashboard page to get an overview
of the app's key metrics, trends, and insights
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 538
12.6 Publishing Your App to Google Play
(2 of 4)
Figure 12-15 All apps page on
Google Play Console
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 539
12.6 Publishing Your App to Google Play
(3 of 4)
Figure 12-16 Create app
website
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 540
12.6 Publishing Your App to Google Play
(4 of 4)
Figure 12-17 Google Play
developers Dashboard
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 541
Activity 12.2: Knowledge Check
1. How much does it cost to register as a developer with Google Play?
2. What are the pixel dimensions of an application icon?
3. In what format should application icons be stored?
4. What is the maximum supported file size for the .aab file at Google Play?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 542
Activity 12.2: Knowledge Check Answers
1. How much does it cost to register as a developer with Google Play?
Answer: $25
2. What are the pixel dimensions of an application icon?
Answer: 512 x 512
3. In what format should application icons be stored?
Answer: PNG (32-bit .png file)
4. What is the maximum supported file size for the .aab file at Google Play?
Answer: 150 MB
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 543
Self-Assessment
1. Now that you have completed the lessons in this textbook, how well prepared
do you feel to write and publish your own Android apps?
2. How prepared do you feel to join a team of professional developers working
on an Android app?
3. What aspects of Android app development would you like to study further or
gain greater experience with to improve your relevant skills?
Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part. 544
Summary
• Click the link to review the objectives for this presentation.
Chapter objectives

Hoisington_Android_4e_PPT_CH01.pptx

  • 1.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 1 1 Android Bootcamp for Developers Using Java, 4e Chapter 1: Voilà! Meet the Android Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 2.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2 Icebreaker • The class will be broken up into pairs of students. • Each student will create three questions to ask their partner: − One true/false question − One question that can be answered with a number − One question that requires a short answer • Partners will take turns asking the questions they’ve prepared. • Each student will then introduce their partner to the class using the information they’ve learned.
  • 3.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 01.01 Describe the market for Android applications • 01.02 Identify the role of the Android platform in the mobile market • 01.03 Describe the features of the Android phone • 01.04 Identify the languages used in Android development • 01.05 Describe the role of Google Play in the marketplace Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 4.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 01.06 Understand the connection between Windows and Android • 01.07 Create an Android project using Android Studio • 01.08 Design the user interface layout within the virtual device • 01.09 Execute an Android application on an emulator • 01.10 Open a saved Android project in Android Studio Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 5.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5 1.1 Understanding the Market for Android Applications • Two primary mobile operating systems − iOS for iPhone − Google Android • Recently, the Google Android phone has become the sales leader • Apps: mobile applications created for smartphones
  • 6.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6 1.2 The Role of the Android in the Mobile Market • Open-Source Platform − Android is an open-source operating system—no company or individual defines the features or direction of the development − Open Handset Alliance: an open-source business alliance of 80 firms that develop standards for mobile devices • Android Open-Source Devices − Android smartphones, tablets, and smartwatches are sold by many companies − As you develop an app, you test it using an emulator, which duplicates how the app looks and feels on a particular device
  • 7.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7 1.3 Features of the Android Phone (1 of 3) Feature Description 3D graphics The interface can support 3D graphics for a 3D interactive game experience or 3D image rendering. Split screen A split-screen, multiwindow feature displays more than one Android application at the same time. Watch a movie on the left side and hold a video call with your friends who are also watching the movie. Dark mode The Dark mode feature helps to save the phone’s battery life. Blank white space consumes more power and drains the battery. Facial recognition Android provides this high-level feature for automatically identifying or verifying a person’s face from a digital image or a video frame. Front- and rear-facing camera Android phones can use either a front- or rear-facing camera, allowing developers to create applications involving video calling.
  • 8.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8 1.3 Features of the Android Phone (2 of 3) Feature Description Multiple language voice-to-text support Android supports multiple human languages. Android provides a feature of converting text to speech in different languages. Text messages can be read aloud to help people who are visually impaired. On-screen keyboard The on-screen keyboard offers suggestions for spelling corrections as well as options for completing words you start typing. The on-screen keyboard also supports voice input. Power management Android identifies programs running in the background that are using memory and processor resources. You can close those apps to free up the phone’s processor memory, extending the battery power. Gaming For optimized gaming, Android supports the use of a gyroscope, gravity and barometric sensors, linear acceleration, and rotation vectors, which provide game developers with highly sensitive and responsive controls.
  • 9.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9 1.3 Features of the Android Phone (3 of 3) Feature Description Voice-based recognition Android recognizes voice actions for calling, texting, and navigating with the phone. Wi-Fi Internet tethering Android supports tethering, which allows a phone to be used as a wireless or wired hotspot that other devices can use to connect to the Internet.
  • 10.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10 1.4 Languages Used in Android Development (1 of 4) • Java is an object-oriented programming language patterned after the C++ language • Java and Kotlin can be used to code Android apps • Android Studio − An integrated development environment (IDE) for building and integrating application development tools and open-source projects − Includes the Android Software Development Kit (SDK) − XML is used to assist in the layout of the Android emulator
  • 11.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11 1.4 Languages Used in Android Development (2 of 4) Figure 1-4 Android Studio running Java code
  • 12.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12 1.4 Languages Used in Android Development (3 of 4) • Android Emulator − Design, develop, prototype, and test Android apps without using a physical device − Mimics almost every feature of a real Android handset, except placing phone calls • Versions of Android − First release was 1.0 (September 2008) − Each subsequent release adds features and fixes known bugs
  • 13.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13 1.4 Languages Used in Android Development (4 of 4) • Versions of Android (continued) − Android has adopted a naming system based on treats/desserts − When the revision level reached 10.0, Android started emphasizing revision number over names − Most recent versions were Android 12.0 (Snow Cone, October 2021) and Android 13.0 (Tiramisu, October 2022)
  • 14.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14 Activity 1.1: Knowledge Check (1 of 2) 1. What is the name of the organization that sets the standards for Android mobile devices? 2. Name at least four types of devices that run the Android OS.
  • 15.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15 Activity 1.1: Knowledge Check (2 of 2) 3. Should a developer target only the most recent version of the Android OS when creating an app? 4. What is the name of the current version of the Android OS?
  • 16.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16 Activity 1.1: Knowledge Check Answers (1 of 2) 1. What is the name of the organization that sets the standards for Android mobile devices? Answer: Open Handset Alliance 2. Name at least four types of devices that run the Android OS. Answer: Smartphones, foldable phones, tablets, watches, MP4 players, automobiles, and Internet TVs
  • 17.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17 Activity 1.1: Knowledge Check Answers (2 of 2) 3. Should a developer target only the most recent version of the Android OS when creating an app? Answer: No. To support as many Android devices as possible, you should set the minimum development version to the lowest available that allows your app to provide its core feature set. 4. What is the name of the current version of the Android OS? Answer: Android 13.0 (internal name: Tiramisu)
  • 18.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18 1.5 Google Play in the Marketplace (1 of 2) • The Android platform consists of the Android OS, application development tools, and a marketplace • Apps are compiled into package files with an .apk extension • Google Play (http://play.google.com) sells and deploys all apps • Programs must meet minimum standards, including: − Reliability − Efficiency − Performance
  • 19.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19 1.5 Google Play in the Marketplace (2 of 2) • Developers must sign an agreement and pay a one-time registration fee (currently $25) to publish apps on Google Play • Apps offered for free are free to publish • For paid apps, standard split is 85% of sales for the developer and 15% for wireless carriers for the first $1 million of annual earnings selling digital goods or services • Also sold through the Amazon Appstore − Pays developers 80% of the sale price for sold apps and grants 10% of their Appstore revenue as an Amazon Web Services credit
  • 20.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20 1.6 The Connection between Windows and Android • Beginning with Windows 11, you can download Android apps to your PC from the Microsoft Store • Android apps are also available for Windows-based tablets and other touch devices • Phone Link App − The Microsoft Phone Link app allows you to connect an Android phone to a Windows 11 PC − Enables text messaging, notifications, and transfer of photos from the phone on the PC
  • 21.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21 1.7 Creating an Android Project Using Android Studio (1 of 8) • Install Android Studio from developer.android.com/studio • Opening Android Studio to Create a New Project − Name: the human-readable title for your app − Package name: the Java package namespace for the source code − Save location on your hard drive or USB drive − Minimum SDK: the minimum API level required by your app − Language (Java or Kotlin)
  • 22.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22 1.7 Creating an Android Project Using Android Studio (2 of 8) • Creating the Hello Android World Project − Android Studio facilitations app creation for various form factors − Two most common projects: Empty Activity and Basic Project − To create a new Android project, you open Studio, click the New Project button, and make selections and entries for the project specs
  • 23.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23 1.7 Creating an Android Project Using Android Studio (3 of 8) Figure 1-8 Welcome to Android Studio window
  • 24.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 24 1.7 Creating an Android Project Using Android Studio (4 of 8) Figure 1-9 New Project window with Empty Activity
  • 25.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25 1.7 Creating an Android Project Using Android Studio (5 of 8) Figure 1-10 Creating Empty Activity
  • 26.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26 1.7 Creating an Android Project Using Android Studio (6 of 8) Figure 1-11 Android Studio project view
  • 27.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27 1.7 Creating an Android Project Using Android Studio (7 of 8) • Building the User Interface − The user interface must be intuitive − The user interface must not distract from functionality − Java code or XML layout files are needed − The XML method is preferred because it allows you to design a user interface without writing large amounts of code
  • 28.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28 1.7 Creating an Android Project Using Android Studio (8 of 8) • The Android Project View − The Android project view contains the key source application folders for a project: manifests, java, and res − The manifests folder includes the AndroidManifest.xml file, which contains all the information about the application that Android needs to run − The java folder contains the Java code source files − The res folder contains resources the app may need: images, music, video, and the user interface named activity_main.xml
  • 29.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29 1.8 The User Interface Layout within the Virtual Device (1 of 12) • The Android SDK includes layout files called activity_main.xml in the res/layout folder of the app • The Layout Editor contains the Palette, Component Tree, toolbar, Design editor, and Attributes pane • Layout: a container that holds as many widgets as needed • Widget: a single element (a.k.a. object), such as a Button from the Palette
  • 30.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30 1.8 The User Interface Layout within the Virtual Device (2 of 12) Figure 1-12 Coding window in Android Studio
  • 31.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31 1.8 The User Interface Layout within the Virtual Device (3 of 12) • The Attributes pane contains the attributes and settings of the currently active app project or object • Attribute: a characteristic of an object that describes what that object can do • Android Studio displays an emulator configuration called the Android Virtual Device (AVD) for design and layout purposes • To change the virtual device, you open the activity_main.xml file to open the Layout Editor, then click the Device for Preview (D) button and select the desired device
  • 32.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32 1.8 The User Interface Layout within the Virtual Device (4 of 12) Figure 1-13 Layout Editor for emulator layout design
  • 33.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33 1.8 The User Interface Layout within the Virtual Device (5 of 12) Figure 1-14 Device for Preview button
  • 34.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34 1.8 The User Interface Layout within the Virtual Device (6 of 12) Figure 1-15 Generic phone selected for Device for Preview
  • 35.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35 1.8 The User Interface Layout within the Virtual Device (7 of 12) • When you select the default TextView component, its text is displayed in the Component Tree − The Component Tree displays widgets in the order they were placed on the emulator • Within the Common Attributes section of the Attributes pane, you can see the text attribute of the component, which is the text it contains
  • 36.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36 1.8 The User Interface Layout within the Virtual Device (8 of 12) Figure 1-16 Design editor
  • 37.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 37 1.8 The User Interface Layout within the Virtual Device (9 of 12) Figure 1-17 Attributes pane
  • 38.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38 1.8 The User Interface Layout within the Virtual Device (10 of 12) • The TextView Component − Text can be updated by:  Changing the hard-coded value for the text attribute in the Attributes pane  Modifying resource files  Changing the application XML source code − It is good programming practice to add an id attribute to each component (object) for use in Java code
  • 39.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 39 1.8 The User Interface Layout within the Virtual Device (11 of 12) Figure 1-18 Emulator with TextView control and text attribute
  • 40.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 40 1.8 The User Interface Layout within the Virtual Device (12 of 12) Figure 1-19 The id attribute
  • 41.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 41 1.9 Executing an Android Application on an Emulator (1 of 2) • The Android emulator (virtual device) runs slowly − Even when idling, it consumes significant CPU time • To run and automatically save your app, click the Run button • Click the Terminate button to stop the emulator from running
  • 42.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 42 1.9 Executing an Android Application on an Emulator (2 of 2) Figure 1-20 Hello Android World app displayed in emulator
  • 43.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 43 Activity 1.2: Knowledge Check 1. In Android Studio, objects such as buttons and text boxes are called _____. 2. Java code source files are stored in the _____ folder. 3. Testing the app is done in a(n) _____.
  • 44.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 44 Activity 1.2: Knowledge Check Answers 1. In Android Studio, objects such as buttons and text boxes are called _____. Answer: widgets 2. Java code source files are stored in the _____ folder. Answer: java 3. Testing the app is done in a(n) _____. Answer: emulator
  • 45.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 45 1.10 Opening a Saved Android Project in Android Studio • To load a saved project, open Studio and then choose the project to open − Click on it in the Recent Projects column or − Click Open an existing Android Project in the Quick Start column, navigate to it, and click the OK button
  • 46.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 46 Self-Assessment 1. After reading about the market and requirements for software development for Android devices, what skills would you like to gain or improve before you begin developing Android apps for publication? What aspects of the market for these apps would you like to learn more about? 2. What is your initial impression of the Android Studio and Android SDK tools available to you as an Android developer?
  • 47.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 47 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 48.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 48 48 Android Bootcamp for Developers Using Java, 4e Chapter 2: Simplify! The Android User Interface Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 49.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 49 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 02.01 Develop a user interface using the TextView, ImageView, and Button components • 02.02 Add text using the String table in the Translations Editor • 02.03 Construct the user interface using a ConstraintLayout • 02.04 Plan out program design • 02.05 Create multiple Android activities Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 50.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 50 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 02.06 View activities in the Android Manifest file • 02.07 Write code using the onCreate( ) method • 02.08 Create a Button event handler • 02.09 Code a Button event handler • 02.10 Correct errors in code • 02.11 Run the completed app in the emulator Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 51.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 51 2.1 Designing an Android App (1 of 5) Figure 2-1 Easy Recipes Android app (left) Figure 2-2 Second screen with recipe (right)
  • 52.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 52 2.1 Designing an Android App (2 of 5) • Designing apps is like constructing a building • The Big Picture: To create the Easy Recipes app, you follow a set of steps that you repeat every time you create an Android application − Create the user interface (XML layout) for every screen − Create a Java class (Activity) for every screen − Code each Java class with appropriate objects and actions − Test the application in the Android phone emulator
  • 53.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 53 2.1 Designing an Android App (3 of 5) • Using the Android User Interface − Interface: the space where interactions occur between people and the app − Android apps run on various form factors − The layout is stored as XML code  Special Android-formatted XML code is extremely compact  XML is a set of instructions for placing components on the screen
  • 54.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 54 2.1 Designing an Android App (4 of 5) Figure 2-4 Creating an empty activity for the Easy Recipes app
  • 55.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 55 2.1 Designing an Android App (5 of 5) Figure 2-5 Android Studio open and ready for you to design the Easy Recipes app
  • 56.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 56 2.2 Using the String Table in the Translations Editor (1 of 5) • The Translations Editor in Android Studio can display the original text in the language in which it was programmed, or the language of the current locale plus culturally specific settings • The String Table − Android provides the strings.xml file or String table inside the Resources folder − strings.xml: a default file that is part of every Android application and contains commonly used strings for an application − string: a series of alphanumeric characters that can include spaces
  • 57.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 57 2.2 Using the String Table in the Translations Editor (2 of 5) • The String Table (continued) − Localization: the use of the String table to change text based on the user’s preferred language − Every string is composed of a key (a.k.a. id attribute), which is the name of a component, and a default value, which is the text associated with the component − To create an application that is accessible to those with visual disabilities, add descriptions to the user interface components that can be read out loud to your user by a speech-based accessibility service such as a screen reader
  • 58.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 58 2.2 Using the String Table in the Translations Editor (3 of 5) Figure 2-7 The strings.xml tab displaying XML code
  • 59.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 59 2.2 Using the String Table in the Translations Editor (4 of 5) Figure 2-10 Adding another string to the String table
  • 60.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 60 2.2 Using the String Table in the Translations Editor (5 of 5) Figure 2-11 Completed String table on the Translations Editor tab
  • 61.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 61 2.3 Constructing the User Interface with the ConstraintLayout (1 of 18) • The Android user interface includes a layout resource designer called the ConstraintLayout in the Component Tree − Organizes how components appear on the app’s screens − Provides more flexibility in positioning components than other layouts − Constraints: the building blocks in an Android layout that attach or align a component to the screen, either by attaching it vertically and horizontally to the edges of the screen or aligning it with another component (or parent) on the screen − Constraints are used to create a Responsive Design UI
  • 62.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 62 2.3 Constructing the User Interface with the ConstraintLayout (2 of 18) • Constraint Handles − Four circular dots on the edges of a selected component − A Constraint component must have at least two constraints—a vertical and a horizontal one − If you drag all four Constraint handles to the four edges of the emulator screen, you can then drag the component anywhere on the screen and it will stay in that location when you run the app − In the ConstraintLayout, a nonconstrained component will float to the top of the screen when the app is run
  • 63.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 63 2.3 Constructing the User Interface with the ConstraintLayout (3 of 18) • Constraint Handles (continued) − A white Constraint handle means that the component is not aligned − A blue Constraint handle means that the component is aligned with the screen or another component − Corner boxes resize the component Figure 2-12 The four circles are Constraint handles
  • 64.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 64 2.3 Constructing the User Interface with the ConstraintLayout (4 of 18) • XML Code − After you move the Constraint handles in the Design view, Android Studio updates your layout resources as XML code in the res/layout resource directory − Other common layouts used in Android Studio include:  LinearLayout  RelativeLayout  TableLayout  GridView
  • 65.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 65 2.3 Constructing the User Interface with the ConstraintLayout (5 of 18) Figure 2-13 ConstraintLayout XML code
  • 66.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 66 2.3 Constructing the User Interface with the ConstraintLayout (6 of 18) • The Easy Recipes Opening User Interface: To change a component's attributes, select it and use the Attributes pane • Android Text Attributes − The text attribute uses text from a string resource to display within the component − The textSize attribute can use various units of measurement  The preferred unit is often sp (scaled-independent pixels)  Other units: inches (in), millimeters (mm), pixels (px), density- independent pixels (dp or dip)
  • 67.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 67 2.3 Constructing the User Interface with the ConstraintLayout (7 of 18) Figure 2-16 The text attribute with @string used to display the String table
  • 68.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 68 2.3 Constructing the User Interface with the ConstraintLayout (8 of 18) Figure 2-17 TextView text displayed in emulator after @string selected
  • 69.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 69 2.3 Constructing the User Interface with the ConstraintLayout (9 of 18) Figure 2-18 Updating the textSize of the TextView component
  • 70.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 70 2.3 Constructing the User Interface with the ConstraintLayout (10 of 18) • Adding a File to the Resources Folder − Graphics used in ImageView components must first be placed in the res/drawable folder − Android supports .png (preferred), .jpg (acceptable), and .gif (discouraged) graphic formats − Android categorizes device screens based on size and density − Android creates a Drawable resource for images when you save them in the res/drawable folder
  • 71.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 71 2.3 Constructing the User Interface with the ConstraintLayout (11 of 18) Figure 2-19 The sorbet.png file copied into the drawable folder within the res folder
  • 72.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 72 2.3 Constructing the User Interface with the ConstraintLayout (12 of 18) • The ImageView Component − An ImageView component can display an icon, a company logo, or a graphic on the Android screen − Reminder: The image to be used should be placed in a drawable resources folder before the ImageView component is placed in the emulator
  • 73.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 73 2.3 Constructing the User Interface with the ConstraintLayout (13 of 18) Figure 2-23 ImageView component with image displayed in the emulator and the id entered
  • 74.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 74 2.3 Constructing the User Interface with the ConstraintLayout (14 of 18) Figure 2-24 The contentDescription set for accessibility
  • 75.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 75 2.3 Constructing the User Interface with the ConstraintLayout (15 of 18) • Adding a Button Component − Button components are commonly used in GUIs − The Android SDK includes four types of button components: Button, FloatingActionButton, ToggleButton, and ImageButton − backgrountTint: an attribute that can be added to a component to set a background color − textColor: an attribute that sets the color of the text in a component
  • 76.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 76 2.3 Constructing the User Interface with the ConstraintLayout (16 of 18) Figure 2-26 Button component placed on emulator and the id named
  • 77.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 77 2.3 Constructing the User Interface with the ConstraintLayout (17 of 18) Figure 2-27 Button displaying larger text
  • 78.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 78 2.3 Constructing the User Interface with the ConstraintLayout (18 of 18) Figure 2-28 Button with colors displayed for background tint color
  • 79.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 79 Activity 2.1: Knowledge Check (1 of 2) 1. List the major steps you follow to create an Android application. 2. Android loads text resources from the project's _____. 3. Which Android layout provides flexibility in positioning components and facilitates making your layout a Responsive Design UI?
  • 80.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 80 Activity 2.1: Knowledge Check (2 of 2) 4. Which unit of measurement is often preferred for the textSize attribute? Why? 5. To display an illustration or photo in an Android app you use a(n) _____.
  • 81.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 81 Activity 2.1: Knowledge Check Answers (1 of 2) 1. List the major steps you follow to create an Android application. Answer: (1) Create the user interface (XML layout) for each screen. (2) Create a Java class (Activity) for every screen. (3) Code each Java class with the appropriate objects and actions. (4) Test the application in the emulator. 2. Android loads text resources from the project's _____. Answer: string table or strings.xml file 3. Which Android layout provides flexibility in positioning components and facilitates making your layout a Responsive Design UI? Answer: ConstraintLayout
  • 82.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 82 Activity 2.1: Knowledge Check Answers (2 of 2) 4. Which unit of measurement is often preferred for the textSize attribute? Why? Answer: sp (scaled-independent pixels), because it will scale the font to meet the user's size preference 5. To display an illustration or photo in an Android app you use a(n) _____. Answer: ImageView component
  • 83.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 83 2.4 Planning Out Program Design • Phases in the program development life cycle: − Gather and analyze the program requirements − Design the user interface − Design the program processing objects − Code the program − Test the program • Creating Activities − Activity: a screen, constructed from XML files and a Java class
  • 84.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 84 2.5 Creating Multiple Android Activities (1 of 2) • A class describes a group of objects and serves as a blueprint, or template, for creating those objects • An object is a specific, concrete instance of a class • When you create an object, you instantiate it, meaning you define one particular variation of the object within a class • Each screen represents one Activity, and each Activity must have a matching Java class file within the java folder • The convention is to use Pascal case for Java class names
  • 85.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 85 2.5 Creating Multiple Android Activities (2 of 2) Figure 2-34 New Recipe class in the Easy Recipes project
  • 86.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 86 2.6 Viewing Activities in the Android Manifest File (1 of 3) • The Android Manifest file (AndroidManifest.xml) contains: − The name of the Java application − A listing of each activity − Permissions needed to access other Android functions (such as the Internet, services, and broadcast receivers) − The minimum level of the Android API • Android Studio automatically adds each activity you create to the Android Manifest file
  • 87.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 87 2.6 Viewing Activities in the Android Manifest File (2 of 3) Figure 2-35 AndroidManifest.xml
  • 88.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 88 2.6 Viewing Activities in the Android Manifest File (3 of 3) • Coding the Android Activity − The entry point of the Activity class is the onCreate() event handler method  Where you initialize the Activity by placing it on the top of Android's stack of Activities − Method: a set of Java statements that can be included inside a Java class; a piece of code that performs some type of action − Method body: a collection of statements that defines what the method does
  • 89.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 89 2.7 Writing Code Using the onCreate() Method (1 of 2) • Syntax for the onCreate() method: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } • This method has a void return type, which means it does not return anything • The Java code necessary to display the content of a specific screen is called SetContentView, which has the following syntax: setContentView(R.layout.activity_main);
  • 90.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 90 2.7 Writing Code Using the onCreate() Method (2 of 2) Figure 2-36 MainActivity.java code for onCreate( ) method
  • 91.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 91 2.8 Creating a Button Event Handler (1 of 3) • Event handler: a part of a program coded to respond to a specific event • Tapping a button is called a click event in Java code • Java code for an event handler must contain the following sections: − A class attribute to hold a reference to the Button object: Button = (Button)findViewById(R.id.btRecipe);  Includes the findViewById() method to find a layout XML file − An OnClickListener() method to await the button click action's onClick() method to respond to the click event
  • 92.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 92 2.8 Creating a Button Event Handler (2 of 3) • When you import the Button type as an Android widget, the button package becomes available throughout the app − An import statement makes more Java functions available to your app • After the Button attribute is referenced in MainActivity.java, an OnClickListener() method is necessary to detect when the user taps an on-screen button • When an OnClickListener is placed in the code window, Java creates an onClick auto-generated stub—a piece of code that serves as a placeholder to declare itself
  • 93.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 93 2.8 Creating a Button Event Handler (3 of 3) • Syntax for an OnClickListener() method that listens for a Button: button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub } }); • The startActivity() method creates an intent to start another Activity − Two parameters: context and name of Activity to open − Syntax: startActivity(new Intent(MainActivity.this, Recipe.class));
  • 94.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 94 2.9 Coding a Button Event Handler (1 of 3) Figure 2-38 Button instantiated with Button library added
  • 95.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 95 2.9 Coding a Button Event Handler (2 of 3) Figure 2-40 The setOnClickListener added with auto-complete feature
  • 96.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 96 2.9 Coding a Button Event Handler (3 of 3) Figure 2-41 Complete code for MainActivity.java— startActivity will launch the second screen’s Recipe.class when the user clicks the button
  • 97.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 97 Activity 2.2: Knowledge Check (1 of 2) 1. Name the phases of the development life cycle in order. 2. The file included in each Android application that provides essential information such as the minimum API level to the Android device is called _____.
  • 98.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 98 Activity 2.2: Knowledge Check (2 of 2) 3. Which Java method initializes an Android Activity? 4. A part of a program that responds to a user interaction such as tapping an on- screen button is called a(n) _____. 5. Which Java method is necessary to detect a user interaction with the app?
  • 99.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 99 Activity 2.2: Knowledge Check Answers (1 of 2) 1. Name the phases of the development life cycle in order. Answer: (1) Gather and analyze the program requirements. (2) Design the user interface. (3) Design the program processing objects. (4) Code the program. (5) Test the program. 2. The file included in each Android application that provides essential information such as the minimum API level to the Android device is called _____. Answer: the Android Manifest or AndroidManifest.xml
  • 100.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 100 Activity 2.2: Knowledge Check Answers (2 of 2) 3. Which Java method initializes an Android Activity? Answer: onCreate() 4. A part of a program that responds to a user interaction such as tapping an on- screen button is called a(n) _____. Answer: event handler 5. Which Java method is necessary to detect a user interaction with the app? Answer: OnClickListener()
  • 101.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 101 2.10 Correcting Errors in Code Figure 2-42 Syntax error
  • 102.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 102 2.11 Saving and Running the Application • Each time an Android application is tested in the emulator, the programming design and code are automatically saved • Click the toolbar's Save All button to save the project • Click the toolbar’s Run app button to test the application in the emulator − The first time an app is executed, you will be prompted to choose an emulator
  • 103.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 103 Self-Assessment 1. What is your opinion of the Android Studio user interface design tools? Are there any improvements you'd like to see? Which tools do you anticipate using most often when creating or updating Android apps? 2. When creating the app project for Chapter 2, which did you enjoy more: working with the XML layout tools in Studio or writing Java code? How do you expect to use these UI creation skills in your future career?
  • 104.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 104 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 105.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 105 105 Android Bootcamp for Developers Using Java, 4e Chapter 3: Engage! Android User Input, Variables, and Operations Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 106.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 106 Chapter Objectives (1 of 3) When you complete this chapter, you will be able to: • 03.01 Collect user input • 03.02 Add a Text Number component • 03.03 Add a string array • 03.04 Change the background attribute • 03.05 Set the hint attribute for a Text component • 03.06 Develop a user interface using a Spinner component Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 107.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 107 Chapter Objectives (2 of 3) When you complete this chapter, you will be able to: • 3.07 Add a prompt and entries to a Spinner component • 3.08 Code the EditText class for the Text Number component • 3.09 Code the Spinner component • 3.10 Instantiate the Button component and variables • 3.11 Code the getText() method Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 108.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 108 Chapter Objectives (3 of 3) When you complete this chapter, you will be able to: • 3.12 Understand arithmetic operations • 3.13 Code the setText( ) method Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 109.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 109 3.1 Collecting User Input (1 of 4) Figure 3-1 Opening screen for Concert Tickets app
  • 110.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 110 3.1 Collecting User Input (2 of 4) • Processes required to create this application: − Define display text using the Translations Editor and strings.xml file − Set the background color − Define a Text Number component − Define a Spinner component − Convert string data to numeric data − Perform arithmetic operations with user-entered data − Display the results, properly formatted, to the user
  • 111.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 111 3.1 Collecting User Input (3 of 4) • Simplifying User Input − The onscreen keyboard is called a soft keyboard − Input can come in the form of tapping or gestures (using two fingers to pan, rotate, or zoom) − The primary design challenge is to simplify the user experience  Use legible fonts, simplify input, and optimize each device’s capabilities to maximize user experience
  • 112.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 112 3.1 Collecting User Input (4 of 4) • Using Android Text Components − Text entered via a soft or physical keyboard is the most common type of mobile input − Text components can request different input types (e.g., plain text, phone number, various types of numeric values) − The component type can constrain the characters available • User Experience − User experience refers to all aspects of interaction with a device
  • 113.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 113 3.2 Adding a Text Number Component (1 of 3) • You can use the id attribute in the Attributes pane to enter a name that begins with the prefix tv (for TextView) − Use descriptive names like tvTickets • Using the String Table − String items displayed in the components of an app can be stored in the string table (strings.xml) − A string array can be used to store a group of related strings
  • 114.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 114 3.2 Adding a Text Number Component (2 of 3) Figure 3-5 Adding strings in the Translations Editor
  • 115.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 115 3.2 Adding a Text Number Component (3 of 3) Figure 3-6 String table
  • 116.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 116 3.3 Adding a String Array (1 of 2) • A string array defines a string resource of related items in a central location within strings.xml • An item defines an individual entry within a string array • As you type the string array XML code, the Android Studio editor offers suggestions in a panel that can complete a statement
  • 117.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 117 3.3 Adding a String Array (2 of 2) Figure 3-9 The string array named tvGroup with three items added in XML code
  • 118.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 118 3.4 Changing Background Attributes (1 of 4) • The Android background feature can help provide an app's individual flair • The background is white by default • To change it, set the background attribute in the Attributes pane
  • 119.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 119 3.4 Changing Background Attributes (2 of 4) Figure 3-10 Changing the background color
  • 120.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 120 3.4 Changing Background Attributes (3 of 4) Figure 3-11 Title added to emulator
  • 121.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 121 3.4 Changing Background Attributes (4 of 4) Figure 3-12 Adding a Text Number component
  • 122.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 122 3.5 Setting the Hint Attribute for a Text Component (1 of 2) • A hint is a short description of a component that is visible as gray text (also called a watermark) inside a Text Number component or any Text input − Often used to provide guidelines about the input expected in a component for the user • You can change the color of the hint by changing the textColorHint attribute − May be necessary to ensure adequate contrast between the text and background
  • 123.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 123 3.5 Setting the Hint Attribute for a Text Component (2 of 2) Figure 3-15 Warning symbol removed from Text Number component
  • 124.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 124 Activity 3.1: Knowledge Check 1. If a component holds multiple text strings, it is best to store them in a(n) _____. 2. To change the screen color for an activity in your app, you set the _____. 3. When you see gray text describing what sort of data to enter within a text entry box in an app, you are looking at a(n) _____.
  • 125.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 125 Activity 3.1: Knowledge Check Answers 1. If a component holds multiple text strings, it is best to store them in a(n) _____. Answer: string array 2. To change the screen color for an activity in your app, you set the _____. Answer: background attribute 3. When you see gray text describing what sort of data to enter within a text entry box in an app, you are looking at a(n) _____. Answer: hint
  • 126.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 126 3.6 Developing a User Interface Using a Spinner Component • A Spinner component is similar to a drop-down list for selecting a single item from a fixed list • The Spinner component displays a prompt with a list of strings called items • Figure 3-16 Spinner dialog box displaying the three concert groups
  • 127.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 127 3.7 Adding a Prompt and Entries to a Spinner Component (1 of 6) • A prompt, which can be used to display instructions at the top of the Spinner control, is stored in strings.xml and is named prompt • Using Entries − The Spinner attribute called entries connects the string array in strings.xml to the Spinner component so the entries can be displayed in the application − The spinnerMode attribute sets the component to dialog mode or drop- down mode − The layout_width and layout_height attributes set the dimensions
  • 128.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 128 3.7 Adding a Prompt and Entries to a Spinner Component (2 of 6) Figure 3-18 Pick a Resource dialog box with tvGroup string array selected
  • 129.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 129 3.7 Adding a Prompt and Entries to a Spinner Component (3 of 6) • Adding the Button, TextView, and ImageView Controls − ImageView components should have a content description for visually impaired users − This description is provided by setting the contentDescription attribute
  • 130.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 130 3.7 Adding a Prompt and Entries to a Spinner Component (4 of 6) Figure 3-20 Adding a Button component
  • 131.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 131 3.7 Adding a Prompt and Entries to a Spinner Component (5 of 6) Figure 3-21 Adding a TextView component
  • 132.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 132 3.7 Adding a Prompt and Entries to a Spinner Component (6 of 6) Figure 3-22 Adding an ImageView component with a contentDescription attribute
  • 133.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 133 3.8 Coding the EditText Class for the Text Number Component (1 of 2) • A Java variable is used to contain data that changes during the execution of a program • A final variable can be initialized only once − Attempting to reassign the value results in a compile error when the application runs • For the concert tickets app, you insert code to create the tickets variable containing the extracted EditText class text: final EditText tickets = (EditText)findViewById(R.id.tvTickets);
  • 134.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 134 3.8 Coding the EditText Class for the Text Number Component (2 of 2) Figure 3-23 EditText code for Text Number component
  • 135.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 135 3.9 Coding the Spinner Control Figure 3-24 Spinner component code
  • 136.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 136 3.10 Instantiating the Button Component and Variables (1 of 6) • An OnClickListener waits in the background and listens for the user to click a button • Clicking the button activates a Button event, triggering the Button event OnClick handler to execute the associated code • Code to instantiate the TextView component to a variable named result is used to display the result of the ticket price calculation in the concert tickets app: final TextView result = ((TextView)findViewById(R.id.tvResult));
  • 137.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 137 3.10 Instantiating the Button Component and Variables (2 of 6) Figure 3-27 TextView component code for the result
  • 138.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 138 3.10 Instantiating the Button Component and Variables (3 of 6) • Declaring Variables − Two steps are necessary in order to use a variable: (1) declare the variable and (2) assign a value to the variable − The declared type of a variable determines which operations are allowed − Java has eight built-in primitive (simple) types of data • Primitive Data Types − Java requires all variables to have a data type
  • 139.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 139 3.10 Instantiating the Button Component and Variables (4 of 6) Type Meaning Range Default Value byte Often used with arrays –128 to 127 0 short Often used with arrays –32,768 to 32,767 0 int Most commonly used number value –2,147,483,648 to 2,147,483,647 0 long Used for numbers that exceed int –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0 float A 32-bit floating-point number +/–3.40282347 ^38 0 double Most common for decimal values +/–1.79769313486231570 ^308 0 char Used for single characters Characters 0 boolean Used for conditional statements True or false False
  • 140.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 140 3.10 Instantiating the Button Component and Variables (5 of 6) • Primitive Data Types (continued) − Sample variable initialization and declarations: double costPerTicket = 89.99; int numberOfTickets; double totalCost; String groupChoice; • The String Data Type − The String type is a class and not a primitive data type − A string can be a character, word, or phrase
  • 141.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 141 3.10 Instantiating the Button Component and Variables (6 of 6) • The String Data Type (continued) − Most strings used in Java are objects of type String (e.g., String groupChoice;) • Declaring the Variables in the Project − Typically declared at the beginning of an activity − Variables must be declared before you can use them
  • 142.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 142 3.11 Coding the getText() Method (1 of 2) • Data stored in the EditText component can be read with the getText() method • Data is read in as a string, by default • A Parse class is used to convert strings into numbers − Parse types include Integer.parseInt(), Float.parseFloat(), Double.parseDouble(), and Long.parseLong() • Sample code to extract a string and convert it into an integer: numberOfTickets = Integer.parseInt(tickets.getText().toString());
  • 143.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 143 3.11 Coding the getText() Method (2 of 2) • Working with Mathematical Operations − The ability to perform arithmetic operations is fundamental to many applications − Most programs require arithmetic operations to add, subtract, multiply, and divide numeric data
  • 144.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 144 Activity 3.2: Knowledge Check 1. A drop-down list for an Android app is created using a(n) _____. 2. The attribute that connects the choices for a drop-down list to their component is called _____. 3. Which Java keyword prevents a variable from being reassigned? 4. Which Java method reads text stored in an EditText component?
  • 145.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 145 Activity 3.2: Knowledge Check Answers 1. A drop-down list for an Android app is created using a(n) _____. Answer: Spinner component 2. The attribute that connects the choices for a drop-down list to their component is called _____. Answer: entries 3. Which Java keyword prevents a variable from being reassigned? Answer: final 4. Which Java method reads text stored in an EditText component? Answer: getText()
  • 146.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 146 3.12 Understanding Arithmetic Operators (1 of 3) • Java's arithmetic operators are + (addition), – (subtraction), * (multiplication), / (division), % (remainder), ++ (increment 1), and -- (decrement 1) • The order of operations determines the sequence for performing calculations when multiple operators are included in an expression − Parentheses; ++ & --; *, /, & %; + & – (left to right) • Formatting Numbers − Java includes a class called DecimalFormat that provides patterns for formatting numbers for output on the Android device
  • 147.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 147 3.12 Understanding Arithmetic Operators (2 of 3) Figure 3-30 Calculating and formatting the ticket cost
  • 148.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 148 3.12 Understanding Arithmetic Operators (3 of 3) • Displaying Android Output: Computed results must be displayed • Using the getSelectedItem() Method − The getSelectedItem() method returns the text label of the currently selected Spinner item − Sample code that assigns a selected item to a String variable named groupChoice: groupChoice = group.getSelectedItem( ).toString( );
  • 149.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 149 3.13 Coding the setText() Method • The setText() method displays text in a TextView component • You can concatenate text and string variable values using +, e.g., String mobile = "Android"; String completeSentence = mobile + " is a platform."; • Sample statement using the setText() method to display text in a TextView component named result: result.setText("Cost for " + groupChoice + " is " + currency.format(totalCost));
  • 150.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 150 Self-Assessment 1. At this point, how comfortable are you using Java classes such as String and Parse to accomplish the goals of a program? What else would you like to learn about these Java classes? 2. Consider the Java methods used to code the sample concert ticket application. How do you anticipate using these methods for your own app development? What would their most common use cases be for you?
  • 151.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 151 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 152.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 152 152 Android Bootcamp for Developers Using Java, 4e Chapter 4: Explore! Icons and Decision-Making Components Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 153.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 153 Chapter Objectives (1 of 3) When you complete this chapter, you will be able to: • 04.01 Create an Android project with a custom icon • 04.02 Display the Action bar icon using code • 04.03 Place a RadioGroup and RadioButtons in Android applications • 04.04 Determine layouts with the layout:margin attributes • 04.05 Align components using the foregroundGravity attributes Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 154.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 154 Chapter Objectives (2 of 3) When you complete this chapter, you will be able to: • 04.06 Add a RadioButton Group • 04.07 Write code for a RadioButton component • 04.08 Make decisions with conditional statements • 04.09 Make decisions using an If statement • 04.10 Make decisions using an If Else statement Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 155.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 155 Chapter Objectives (3 of 3) When you complete this chapter, you will be able to: • 04.11 Make decisions using logical operators • 04.12 Display an Android toast notification • 04.13 Use the isChecked() method with RadioButtons • 04.14 Make decisions using nested If statements Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 156.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 156 4.1 Creating an Android Project with a Custom Icon (1 of 12) • We will be creating an app to convert pounds to kilograms and kilograms to pounds • App is designed to be used in a hospital setting to administer medication to patients based on patient weight • Hospital scales register pounds, whereas medication dosages are based on patient weight in kilograms • Formulas needed: − Kilograms = pounds * 2.2 − Pounds = kilograms / 2.2
  • 157.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 157 4.1 Creating an Android Project with a Custom Icon (2 of 12) Figure 4-1 Opening screen of the Medical Calculator Figure 4-2 Results screen of the Medical Calculator
  • 158.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 158 4.1 Creating an Android Project with a Custom Icon (3 of 12) • Steps to complete the app: − Create a customized launcher icon − Use code to add the icon for display in the Action bar − Define a TextField for the data entry of the weight of the patient − Define a RadioGroup to select a conversion of pounds to kilograms or kilograms to pounds − Display a toast message for data validation − Convert data so it can be used for arithmetic operations
  • 159.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 159 4.1 Creating an Android Project with a Custom Icon (4 of 12) • Steps to complete the app (continued): − Perform arithmetic operations on data the user enters − Display formatted results
  • 160.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 160 4.1 Creating an Android Project with a Custom Icon (5 of 12) • Using the Launcher Icon − The launcher icon opens the app from the home screen and represents the app when it is published to Google Play − A high-quality launcher icon can influence users to purchase your app − Icons can establish brand identity − Simple images with clear visual cues have a memorable impact − Vector-based graphics are best for icon design because these images can be easily scaled without loss of detail
  • 161.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 161 4.1 Creating an Android Project with a Custom Icon (6 of 12) • Using the Launcher Icon (continued) − Google Play specifies the size and format of all launcher icons − Should be saved with the .png file format/extension − When you publish an app to Google Play, you must provide a 512 × 512- pixel, high-resolution application icon in the developer console as you upload your program  This icon is displayed on the Google Play site to provide a description of the app and does not replace your launcher icon − Google Play's naming convention for launcher icons: ic_launcher
  • 162.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 162 4.1 Creating an Android Project with a Custom Icon (7 of 12) Resolution Dots Per Inch (dpi) Size (px) ldpi (low-density screen) 120 36 × 36 mdpi (medium-density screen) 160 48 × 48 hdpi (high-density screen) 240 72 × 72 xhdpi (extra high-density screen) 320 96 × 96 xxhdpi (extra extra high-density screen) 480 144 × 144 xxxhdpi (extra extra extra high-density screen) 640 192 × 192
  • 163.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 163 4.1 Creating an Android Project with a Custom Icon (8 of 12) • Customizing a Launcher Icon − Image Asset Studio: Android Studio tool that helps you generate your own app icons from images  Generates a set of icons at the appropriate resolution for each pixel density your app supports − Use the built-in Image Asset Studio wizard to create these multiple icons and store them in the correct folders
  • 164.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 164 4.1 Creating an Android Project with a Custom Icon (9 of 12) Figure 4-6 Default launcher icons in the Asset Studio
  • 165.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 165 4.1 Creating an Android Project with a Custom Icon (10 of 12) Figure 4-7 Custom launcher icons
  • 166.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 166 4.1 Creating an Android Project with a Custom Icon (11 of 12) Figure 4-8 Confirmed icon path in Asset Studio displayed in res/mipmap folders
  • 167.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 167 4.1 Creating an Android Project with a Custom Icon (12 of 12) Figure 4-9 Resized custom icons and their pathways in the mipmap folder
  • 168.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 168 4.2 Displaying the Action Bar Icon Using Code (1 of 4) • An Action bar icon is considered a logo that represents what the program’s function is in a single glance − For example, the medical symbol conveys the purpose and identity of the app • Supporting code needed in MainActivity.java to display the image in the Action bar: getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setLogo(R.mipmap.ic_launcher); getSupportActionBar().setDisplayUseLogoEnabled(true);
  • 169.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 169 4.2 Displaying the Action Bar Icon Using Code (2 of 4) Figure 4-11 Icon displayed in running app in the emulator
  • 170.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 170 4.2 Displaying the Action Bar Icon Using Code (3 of 4) • String Table − String resources are stored within the /res/values/strings.xml file − Any strings you add to the strings.xml file are accessible within your application
  • 171.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 171 4.2 Displaying the Action Bar Icon Using Code (4 of 4) Figure 4-12 Translations Editor with new strings in the String table
  • 172.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 172 4.3 Placing a RadioGroup and RadioButtons in Android Applications (1 of 2) • RadioButton components are used to select or deselect an option − Can be arranged horizontally or vertically − Have a label defined by the Text attribute − Checked attribute can be set to true or false − Typically used together in a RadioGroup container  Only one RadioButton in the group can be selected at a time − It saves time to offer a default selection (checked = true) for the option that is used most often 172
  • 173.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 173 4.3 Placing a RadioGroup and RadioButtons in Android Applications (2 of 2) • Changing the Text Color of RadioButton and CheckBox Components − Hexadecimal color code: a triplet of three colors  Example: #FF0000 is a true red  Color system used by the Android platform − Codes range from 00 to FF (00 = none, FF = full) − Codes are identified by a pound sign, followed by the red, green, and blue values contained in the color 173
  • 174.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 174 4.4 Determining Layouts with the layout:margin Attributes • You can change a component's alignment after placing it on the UI by adjusting its Gravity settings • The layout:margin attribute allows for more flexibility in controlling your layout − You can change all margins equally or change the left, top, right, and bottom margins individually • Set density-independent pixel (dp) values instead of “eyeballing” to create equal spaces around components • Using the same specified margins creates a symmetrical layout
  • 175.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 175 Activity 4.1: Knowledge Check (1 of 2) 1. What is an app's opening icon called? 2. What tool can you use to generate a launcher icon from an image? 3. RadioButton components are typically used together in a(n) _____. 4. What is #FF0000?
  • 176.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 176 Activity 4.1: Knowledge Check (2 of 2) 5. What settings are used to change the alignment of an object? 6. Which attribute can you use to set the spacing around each side of an object individually?
  • 177.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 177 Activity 4.1: Knowledge Check Answers (1 of 2) 1. What is an app's opening icon called? Answer: The launcher icon 2. What tool can you use to generate a launcher icon from an image? Answer: Image Asset Studio 3. RadioButton components are typically used together in a(n) _____. Answer: RadioGroup 4. What is #FF0000? Answer: The hexadecimal color code for a true red color
  • 178.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 178 Activity 4.1: Knowledge Check Answers (2 of 2) 5. What settings are used to change the alignment of an object? Answer: Gravity settings 6. Which attribute can you use to set the spacing around each side of an object individually? Answer: layout:margin attribute
  • 179.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 179 4.5 Aligning Components Using the foregroundGravity Attributes • A component's gravity can be changed using the Attributes pane or with a toolbar shortcut • The foregroundGravity tool changes the linear alignment of a component • Layout gravity is similar to the alignment feature in Microsoft Office that allows a component to snap to the left, center, right, top, or bottom
  • 180.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 180 4.6 Adding the RadioButton Group and Other Components (1 of 5) • When naming a RadioButton with the id attribute, use a name that begins with the prefix rb, which represents a radio button in the code • Completing the User Interface − Remember to create a clean layout and use the entire screen effectively
  • 181.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 181 4.6 Adding the RadioButton Group and Other Components (2 of 5) Figure 4-15 Text Number component and foregroundGravity attribute to center hint text
  • 182.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 182 4.6 Adding the RadioButton Group and Other Components (3 of 5) Figure 4-16 RadioGroup component
  • 183.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 183 4.6 Adding the RadioButton Group and Other Components (4 of 5) Figure 4-17 Two RadioButton components with first RadioButton checked
  • 184.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 184 4.6 Adding the RadioButton Group and Other Components (5 of 5) Figure 4-20 TextView component to display results
  • 185.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 185 4.7 Coding a RadioButton Component (1 of 3) • Java syntax to reference each of the RadioButton components using the findViewById() method: final RadioButton lbToKilo = (RadioButton) findViewById(R.id.rbLbToKilo); final RadioButton kiloToLb = (RadioButton) findViewById(R.id.rbKiloToLb); • Coding the Button Component − The Button component detects user interaction using an event listener
  • 186.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 186 4.7 Coding a RadioButton Component (2 of 3) Figure 4-22 EditText and RadioButtons referenced
  • 187.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 187 4.7 Coding a RadioButton Component (3 of 3) Figure 4-24 OnClickListener for the Button component
  • 188.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 188 4.8 Making Decisions with Conditional Statements • Decision structure: a fundamental computer programming structure that tests a condition and then follows a path specified for the result • A conditional statement tests a condition and returns a result of either true or false − Boolean operators have two values, which are either true (1) or false (0) − When you compare values, Java returns a Boolean value of true or false
  • 189.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 189 4.9 Making Decisions Using an If Statement • An If statement tests a condition and executes the statement(s) between its curly braces only when the condition is true • Syntax for simplest form of the Java If statement: if (condition) { //Statements completed if true } • If the condition is false, program execution continues with the statements after the closing brace
  • 190.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 190 4.10 Making Decisions Using an If Else Statement (1 of 3) • An If Else statement executes one set of instructions if a condition is true and another set of instructions if the condition is false • Java syntax for the If Else statement: if (condition) { //Statements completed if condition is true } else { //Statements completed if condition is false }
  • 191.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 191 4.10 Making Decisions Using an If Else Statement (2 of 3) Relational Operator Meaning Example Resulting Condition == Equal to 6 == 6 True != Not equal to 4 != 7 False > Greater than 3 > 2 True < Less than 8 < 1 False >= Greater than or equal to 5 >= 5 True <= Less than or equal to 9 <= 6 False
  • 192.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 192 4.10 Making Decisions Using an If Else Statement (3 of 3) • Relational Operators − Relational operators are used in conditional statements to express the relationship between the numbers being tested − Java strings are compared using the equals() method − The compareTo() method is used to determine the alphabetical order of strings  Returns a negative integer if the first string precedes the second  Returns 0 if they are equal  Returns a positive integer if the first string follows the second
  • 193.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 193 4.11 Making Decisions Using Logical Operators • When more than one condition is included in an If statement, the conditions are called a compound condition • Java logical operators are needed to control the flow of execution with compound conditions − && means and—all Boolean conditions must be true − || means or—at least one Boolean condition must be true − ! means not—it reverses the meaning of a Boolean condition • Data validation: the practice of checking the integrity, accuracy, and structure of a program before it is used for a business operation
  • 194.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 194 4.12 Displaying an Android Toast Notification • A toast notification communicates messages to the user unobtrusively − Message slides upward into view like toast popping out of a toaster • The toast notification code uses a Toast object and the makeText() method with three parameters: context (activity name), message text, and duration of display • Java syntax: Toast toast = Toast.makeText(context, text, duration).show(); − Duration is LENGTH_SHORT or LENGTH_LONG − The show() method displays the Toast object
  • 195.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 195 4.13 Using the isChecked() Method with RadioButton Components • The isChecked() method determines if a RadioButton (or CheckBox) object has been selected • Example syntax: if (lbToKilo.isChecked) { //Statements completed if condition is true (Boolean value) } else { //Statements completed if condition is false (Boolean value) }
  • 196.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 196 Activity 4.2: Knowledge Check 1. What kind of statement is used to test a radio button's state? 2. What Java method is used to determine whether or not a radio button has been selected? 3. When you use a(n) _____ statement in a decision structure, code is executed for either a true or a false result. 4. What do we use to compare strings to see if they are equal? 5. What are the three common logical operators?
  • 197.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 197 Activity 4.2: Knowledge Check Answers 1. What kind of statement is used to test a radio button's state? Answer: A conditional statement 2. What Java method is used to determine whether or not a radio button has been selected? Answer: isChecked() 3. When you use a(n) _____ statement in a decision structure, code is executed for either a true or a false result. Answer: If Else 4. What do we use to compare strings to see if they are equal? Answer: Java equals() method 5. What are the three common logical operators? Answer: && (And), || (Or), ! and (Not)
  • 198.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 198 4.14 Using Nested If Statements (1 of 5) • When you place an If statement within another If statement, the inner If statement is said to be nested within the outer If statement • Coding the Button Event − The syntax Double.parseDouble converts input to a Double data type − Integer.parseInt converts input to an Integer data type − The DecimalFormat object is used to format data for display
  • 199.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 199 4.14 Using Nested If Statements (2 of 5) Figure 4-26 DecimalFormat code used to round to the nearest tenth
  • 200.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 200 4.14 Using Nested If Statements (3 of 5) • Coding the Nested If Statements − In the Medical Calculator app, nested If statements are used  Outer If statements determine the conversion option selected  Inner If Else statements display the result only if the weight is within 500 pounds or 225 kilograms (the If branch)  If the weight is out of this range, a toast notification is displayed instead (this is the Else branch)
  • 201.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 201 4.14 Using Nested If Statements (4 of 5) Figure 4-31 Completed code for If Else statements
  • 202.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 202 4.14 Using Nested If Statements (5 of 5) Figure 4-32 Running app and toast notification
  • 203.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 203 Self-Assessment 1. How much attention do you pay to the launcher icons for apps that you use? Do you think that launcher icons have impacted your choices regarding which apps to download? 2. What previous experience do you have designing and coding decision structures? Would you have coded the logic for the Medical Calculator app differently? If so, explain why. 3. Describe how you expect to use toast notifications in apps that you develop.
  • 204.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 204 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 205.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 205 205 Android Bootcamp for Developers Using Java, 4e Chapter 5: Investigate! Android Lists, Arrays, Switch Statements, and Web Browsers Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 206.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 206 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 05.01 Create an Android project using a list • 05.02 Add a ListView layout with XML code • 05.03 Create an array • 05.04 Initialize a ListView layout component • 05.05 Display an array in the ListView with an ArrayAdapter Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 207.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 207 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 05.06 Call the setOnItemClick() method • 05.07 Create a decision structure using a Switch statement • 05.08 Describe the purpose of intents in Android • 05.09 Launch a browser from an Android device • 05.10 Test an application with multiple decisions Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 208.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 208 5.1 Creating an Android Project Using a List (1 of 6) • Lists are one of the most common designs in mobile apps − Scrollable − Selectable − Programmable to bring up the next activity (screen) • Figure 5-1 The City Guide Android app
  • 209.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 209 5.1 Creating an Android Project Using a List (2 of 6) • Steps to complete the City Guide app: 1. Create a list using a ListView component. 2. Define an array to establish the items of the list. 3. Add the images used in the project. 4. Define an XML file to design a list. 5. Code a Switch decision structure to handle the selection of items. 6. Open an Android web browser to display a specified Uniform Resource Identifier (URI). 7. Create multiple classes and XML layout files to display pictures of attractions. 209
  • 210.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 210 5.1 Creating an Android Project Using a List (3 of 6) • Beginning the City Guide App − The opening screen of the City Guide app contains a vertical list of attractions − Selecting an item opens up a related web page or an image of the attraction − The TableLayout view is similar to the ListView, but ListView allows you to select each list row for further action − With a ListView component, scrollbar is automatically displayed if there are more items than the screen can display 210
  • 211.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 211 5.1 Creating an Android Project Using a List (4 of 6) Figure 5-4 Application information for the new Android project
  • 212.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 212 5.1 Creating an Android Project Using a List (5 of 6) Figure 5-5 Asset Studio dialog box
  • 213.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 213 5.1 Creating an Android Project Using a List (6 of 6) Figure 5-6 Code to display launcher icon
  • 214.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 214 5.2 Adding a ListView Layout with XML Code (1 of 2) • Two ways to design a layout: − Drag and drop components from the Palette into the emulator window − Code the activity_main.xml file using XML code in the Layout Editor • Studio provides an auto-complete feature to assist in writing XML • For the City Guide project, the ListView component will be added by modifying the activity_main.xml file
  • 215.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 215 5.2 Adding a ListView Layout with XML Code (2 of 2) Figure 5-8 ListView layout
  • 216.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 216 5.3 Creating an Array (1 of 2) • Array: a container object that holds a fixed number of values of a single type • An array variable can store more than one value − You can avoid assigning a separate variable for each item in a list for a ListView component • Each individual item in an array that contains a value is called an element • Arrays provide access to data by using a numeric index, or subscript − Elements of the attraction array are accessed as attraction[0], attraction[1], attraction[2], etc.
  • 217.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 217 5.3 Creating an Array (2 of 2) • Declaring an Array − Declaration includes the array's data type and its name − Sample arrays of numeric and character values: int[] age={18,21,38,88}; double[] weather={72.3, 65.0, 25.7, 99.5}; char[] initials={'P','N','D'}; − Array of strings for the City Guide's ListView component: String[] attraction = new String[]{"Art Institute of Chicago", "Magnificent Mile", "Willis Tower", "Chicago Bean", "Water Tower"};
  • 218.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 218 5.4 Initializing a ListView Layout Component (1 of 2) • You use the Android ListView layout to display the array elements in a scrollable list • The list items are automatically inserted in the ListView component using a Java Adapter that pulls content from a source such as an array or database
  • 219.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 219 5.4 Initializing a ListView Layout Component (2 of 2) Figure 5-11 ListView layout defined
  • 220.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 220 5.5 Displaying an Array in the ListView with an ArrayAdapter (1 of 3) • An adapter provides a data model for the layout of a list and for converting the array data into list items • The ListView and ArrayAdapter work together to display a list • The ArrayAdapter<String> supplies the String array data to the ListView • The three parameters that follow ArrayAdapter refer to: − The this class − A generic layout (simple_list_item_1 for the City Guide) − The array of list items
  • 221.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 221 5.5 Displaying an Array in the ListView with an ArrayAdapter (2 of 3) • Adapter code for the City Guide app: ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, attraction); listView.setAdapter(adapter); • Adding the Images to the Resources Folder − Place images for the app into the res/drawable folder − Remember to follow copyright laws that apply to any copyrighted images you use
  • 222.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 222 5.5 Displaying an Array in the ListView with an ArrayAdapter (3 of 3) • Adding the String Table − The City Guide app needs three strings for the ImageView component descriptions − These are added to the strings.xml file in the resvalues folder using the Translations Editor
  • 223.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 223 Activity 5.1: Knowledge Check 1. What is the name of the component that makes it easy to display a list of items? 2. What are the individual items in an array called? 3. What does an adapter do? 4. Where do images need to be stored?
  • 224.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 224 Activity 5.1: Knowledge Check Answers 1. What is the name of the component that makes it easy to display a list of items? Answer: ListView component 2. What are the individual items in an array called? Answer: elements 3. What does an adapter do? Answer: It provides a data model for the layout of the list and for converting the array data into list items 4. Where do images need to be stored? Answer: In the res/drawable folder
  • 225.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 225 5.6 Calling the setOnItemClick() Method • setOnItemClick() is called when an item from the list is selected • When an attraction in the list is selected, the position of the item is passed from setOnItemClick() and evaluated with a decision structure − The position parameter is assigned an integer value based on the index of the array item selected (0, 1, 2, etc.) • Code calling this method and onItemClick() method stub: listView.setOnItemClickListener(new AdapterView.OnItemClickListener() public void onItemClick(AdapterView<?> parent, View view, int position, long id) { }
  • 226.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 226 5.7 Creating a Decision Structure with a Switch Statement (1 of 3) • Switch statement: a decision structure that allows you to choose from many statements based on an integer or single character (char) input • Switch statement syntax: switch(position){ case 0: //statements that are executed if position == 0 break; case 1: //statements that are executed if position == 1 break; default: //statements that are executed if position != any of the cases }
  • 227.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 227 5.7 Creating a Decision Structure with a Switch Statement (2 of 3) • The switch keyword is followed by: − An integer expression in parentheses − Its cases, all enclosed in braces • The case keyword is followed by a colon and a value • Typically, the statement within a case ends with a break statement − The break statement exits the Switch decision structure and continues with the next statement • If there is no matching case value, the (optional) default option is executed
  • 228.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 228 5.7 Creating a Decision Structure with a Switch Statement (3 of 3) Figure 5-18 Case statements
  • 229.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 229 5.8 Understanding Android Intents • A browser is launched with Android code using an intent • Android intents send and receive activities and services − They allow apps to talk to each other in a very simple way • When the City Guide app sends an intent, the browser app handles the request and opens the website
  • 230.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 230 5.9 Launching a Browser from an Android Device (1 of 6) • Android phones have a built-in browser with an intent filter that accepts intent requests from other apps − The intent sends the browser a URI (Uniform Resource Identifier), a string that identifies web resources − URL (Uniform Resource Locator): a website address − A URI is a URL with additional information necessary for gaining access to the resources required for posting a web page • ACTION_VIEW: a generic action you can use to send any request to get the most reasonable action to occur − The most common action performed on data
  • 231.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 231 5.9 Launching a Browser from an Android Device (2 of 6) • Sample code for launching websites in an Android browser: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://artic.edu"))); • Adding Multiple Class Files − Multiple classes are needed to display images on the screen when the user selects Willis Tower, Chicago Bean, or Water Tower on the opening ListView component − Classes must begin with a capital letter − When you add a class by creating a new activity, a coordinating XML layout file with the same name but a lowercase beginning letter is automatically created
  • 232.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 232 5.9 Launching a Browser from an Android Device (3 of 6) • Designing XML Layout Files − For the City Guide app, XML layout files must be designed to display an ImageView component with an image source file − To do this, you can drag an ImageView component into the emulater − You can add its id attribute, adjust its constraint handles, and select a description from the string table for its contentDescription attribute
  • 233.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 233 5.9 Launching a Browser from an Android Device (4 of 6) Figure 5-25 The activity_bean.xml file
  • 234.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 234 5.9 Launching a Browser from an Android Device (5 of 6) • Coding the Multiple Class Files − The City Guide app must launch the class files when the user selects an item from the ListView component − A startActivity() method for each case in the Switch statement opens the next Activity, which in turn launches the appropriate XML layout that displays an image − Sample call to startActivity(): startActivity(new Intent(MainActivity.this, Bean.class));
  • 235.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 235 5.9 Launching a Browser from an Android Device (6 of 6) Figure 5-27 Complete code for MainActivity.java class [part b]
  • 236.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 236 Activity 5.2: Knowledge Check 1. What Java method, which is called when an item in a ListView component is selected, passes the position of the item for evaluation? 2. A Switch statement attempts to match an integer or character to one of its _____. 3. An Android intent that launches a browser identifies web resources by sending the browser a(n) _____. 4. What is the most common action performed on data in an Android app?
  • 237.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 237 Activity 5.2: Knowledge Check Answers 1. What Java method, which is called when an item in a ListView component is selected, passes the position of the item for evaluation? Answer: setOnItemClick() 2. A Switch statement attempts to match an integer or character to one of its _____. Answer: cases 3. An Android intent that launches a browser identifies web resources by sending the browser a(n) _____. Answer: URI (Uniform Resource Identifier) 4. What is the most common action performed on data in an Android app? Answer: ACTION_VIEW
  • 238.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 238 5.10 Testing an Application with Multiple Decisions • Make sure all the fields can gracefully handle any click or any value entered in any Android app • Click Run on the menu bar, then select Run to test the app in the emulator • Testing an Android app is called usability testing • In addition to the traditional tests of navigation and ease of use, Section 508 compliance is a third component to be tested − The 1998 Amendment to Section 508 of the Rehabilitation Act spells out accessibility requirements for individuals with certain disabilities − For more details, refer to General Services Administration: Section 508 Report
  • 239.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 239 Self-Assessment 1. How much of the Java code in the City Guide app project presented in Chapter 5 was familiar versus unfamiliar to you? Describe your experience working through the coding project and identify any related coding skills that you would like to practice more. 2. Describe any previous coding experience you have with the Switch-case decision structure. In what situations do you expect to use Switch statements in your own coding projects?
  • 240.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 240 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 241.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 241 241 Android Bootcamp for Developers Using Java, 4e Chapter 6: Jam! Implementing Audio in Android Apps Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 242.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 242 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 06.01 Create an Android project using a splash screen • 06.02 Launch the splash screen before the MainActivity • 06.03 Design a TextView component with a background image • 06.04 Create a timer • 06.05 Understand the life and death of an Activity • 06.06 Assign class variables Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 243.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 243 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 06.07 Play music • 06.08 Create a raw folder for music files • 06.09 Play music with a MediaPlayer method • 06.10 Start and resume music playback • 06.11 Change the text attribute of a component • 06.12 Change the visibility of a component Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 244.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 244 6.1 Creating an Android Project Using a Splash Screen (1 of 5) Figure 6-1 Aloha Music Android app Figure 6-2 Music played in the Android app
  • 245.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 245 6.1 Creating an Android Project Using a Splash Screen (2 of 5) • The Aloha Music app displays a splash screen (opening screen) for five seconds, then opens a second screen automatically • Steps to complete the app: − Create a splash screen with a timer. − Design a TextView control with a background image. − Initialize a TimerTask and a timer. − Launch a second Activity. − Design a second XML layout. − Add music files to the raw folder. − Initialize the MediaPlayer class. − Play and pause music with a Button component.
  • 246.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 246 6.1 Creating an Android Project Using a Splash Screen (3 of 5) • Beginning the Aloha Music App − Many app splash screens include the program name, a brand logo, and/or the author's name − Android initializes its resources and loads necessary files while the splash screen is displayed
  • 247.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 247 6.1 Creating an Android Project Using a Splash Screen (4 of 5) Figure 6-3 Setting up the Aloha Music project in the New Project window
  • 248.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 248 6.1 Creating an Android Project Using a Splash Screen (5 of 5) Figure 6-4 Adding the Splash Activity (splash screen)
  • 249.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 249 6.2 Launching the Splash Screen before the MainActivity (1 of 3) • By default, the MainActivity launches first • To launch the SplashActivity (splash screen) first, you must modify the Android Manifest file − Move the first launch activity intent from MainActivity to SplashActivity − Set android:exported property to "true"
  • 250.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 250 6.2 Launching the Splash Screen before the MainActivity (2 of 3) Figure 6-5 Updating the Android Manifest file
  • 251.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 251 6.2 Launching the Splash Screen before the MainActivity (3 of 3) Figure 6-6 Launching the Splash Activity first in the Android Manifest file
  • 252.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 252 6.3 Designing a TextView Component with a Background Image (1 of 3) • You use the TextView component's background attribute to specify a background image − Can display an image or a solid-color fill • The margins and gravity properties are used to place the text within the component • For the Aloha Music app, you must: − Add images to the drawable folder − Enter text strings into the string table using the Translations Editor − Add a TextView Component with a background image to the XML layout file for the splash screen
  • 253.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 253 6.3 Designing a TextView Component with a Background Image (2 of 3) Figure 6-8 Translations Editor
  • 254.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 254 6.3 Designing a TextView Component with a Background Image (3 of 3) Figure 6-9 The activity_splash.xml file displays a TextView component with a background image
  • 255.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 255 6.4 Creating a Timer (1 of 5) • A timer in Java can: − Execute a one-time task such as displaying an opening splash screen − Perform a continuous process such as a morning wake-up call set to run at regular intervals • Timers use two Java classes, TimerTask and Timer • Each time a timer runs, it runs in a single thread: a single sequential flow of control within a program
  • 256.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 256 6.4 Creating a Timer (2 of 5) • The first step in creating a timer is creating a TimerTask object: TimerTask task = new TimerTask( ) { @Override public void run() { // TODO Auto-generated method stub } • The TimerTask invokes a scheduled timer • The TimerTask must implement a run() method that is called by the timer when the task is scheduled for execution
  • 257.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 257 6.4 Creating a Timer (3 of 5) Figure 6-11 The run() method
  • 258.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 258 6.4 Creating a Timer (4 of 5) • Scheduling a Timer − After a reference is included to the TimerTask class, a timer must be scheduled − A delay timer is scheduled in milliseconds using the Timer class: Timer opening = new Timer(); opening.schedule(task,5000);  Line 1: the object named opening initializes a new instance of the Timer class  Line 2: the schedule() method of the Timer class is called, passing in the name of the variable that was initialized for the Timer class and the number of milliseconds  5,000 milliseconds = about 5 seconds
  • 259.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 259 6.4 Creating a Timer (5 of 5) Figure 6-13 Timer scheduled for 5 seconds
  • 260.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 260 6.5 Understanding the Life and Death of an Activity (1 of 7) • Each activity has a life cycle—a series of actions from the beginning of an Activity until its end • When the activity begins, we use the onCreate() method − Initializes the user interface with an XML layout − Sets up the resources required to perform the Activity • When the activity ends, we use the onDestroy() method − Releases the Activity's resources to free up memory • Four states of an Activity: active, paused, stopped, or dead
  • 261.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 261 6.5 Understanding the Life and Death of an Activity (2 of 7) Method Description onCreate() The onCreate() method begins each Activity. This method also provides a Bundle containing the Activity’s previously frozen state, if it had one. onRestart() If the Activity is stopped, onRestart() begins the Activity again. If this method is called, it indicates your Activity is being redisplayed to the user from a stopped state. The onRestart() method is always followed by onStart(). onStart() If the Activity is hidden, onStart() makes the Activity visible. onResume() The onResume() method is called when the user begins interacting with the Activity. The onResume() method always follows onPause(). onPause() This method is called when an Activity is about to resume. onStop() This method hides the Activity. onDestroy() This method destroys the Activity. Typically, the finish() method (part of the onDestroy() method) is used to declare that the Activity is finished; when the next Activity is called, the method releases all the resources from the first Activity.
  • 262.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 262 6.5 Understanding the Life and Death of an Activity (3 of 7) • The finish() method is part of the onDestroy() method − Called when the Activity is completed and should be closed − Typically occurs directly before another Activity is launched • In Figure 6-14: − Rectangles represent methods you can implement to perform operations when the Activity moves between states − Colored ovals are the possible major states of the Activity • In the Aloha Music app, onDestroy() is called from within the timer task's run() method to destroy the SplashActivity before launching the second Activity
  • 263.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 263 6.5 Understanding the Life and Death of an Activity (4 of 7) Figure 6-14 Android life cycle
  • 264.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 264 6.5 Understanding the Life and Death of an Activity (5 of 7) Figure 6-15 After the finish( ) method is called
  • 265.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 265 6.5 Understanding the Life and Death of an Activity (6 of 7) • Launching the Next Activity − An intent must request that the next Activity be launched − For the Aloha Music app, the following line is added to the run() method after the call to finish(): startActivity(new Intent(SplashActivity.this, MainActivity.class)); • Designing the activity_main.xml File − A second XML layout file is displayed when the onCreate() method is called within MainActivity.java − For the Aloha Music app, MainActivity.java uses the default Relative layout with two ImageView and Button components
  • 266.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 266 6.5 Understanding the Life and Death of an Activity (7 of 7) Figure 6-19 The activity_main.xml layout is complete
  • 267.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 267 Activity 6.1: Knowledge Check 1. What do you call an app's opening screen that displays a logo for a few seconds? 2. Which type of object invokes a scheduled timer? 3. How many milliseconds equal approximately 1 second? 4. What are the four possible states of an Activity? 5. Which Android life cycle method will hide the Activity?
  • 268.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 268 Activity 6.1: Knowledge Check Answers 1. What do you call an app's opening screen that displays a logo for a few seconds? Answer: Splash Screen 2. Which type of object invokes a scheduled timer? Answer: TimerTask 3. How many milliseconds equal approximately 1 second? Answer: 1,000 4. What are the four possible states of an Activity? Answer: active, paused, stopped, or dead 5. Which Android life cycle method will hide the Activity? Answer: onStop()
  • 269.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 269 6.6 Assigning Class Variables (1 of 3) • Local variables are declared by variable declaration statements within a method − Local variables effectively cease to exist when method execution is complete • The scope of a variable refers to the variable’s visibility within a class • Variables accessible from anywhere in a class have global scope − When a variable is needed in multiple methods in a class, a global variable is used − Global scope variables are called class variables in Java
  • 270.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 270 6.6 Assigning Class Variables (2 of 3) • For the Aloha Music app, class variables are defined for a Button, a MediaPlayer, and an integer variable (playing) for each Button component's onClick() method • The onCreate() method opens the XML layout and defines two Button components − Each button has a setOnClickListener that implements the Button.OnClickListener, creating an instance of OnClickListener and calling the onClick() method, when the button is tapped − The onClick() method responds to the user's action by playing or pausing the song
  • 271.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 271 6.6 Assigning Class Variables (3 of 3) Figure 6-23 Inserting the second Button OnClickListener stub
  • 272.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 272 6.7 Playing Music • Android phones and tablets include built-in music players • Android includes a MediaPlayer class that can play both audio and music files • Android allows you to play audio and video from several types of data sources: media files stored in the app's resources, stand-alone files, an SD (Secure Digital) memory card, a data stream from the Internet • .mp3 is the most common supported audio file type, but .wav, .ogg, and .midi are typically supported by most Android hardware • Codec: a computer technology used to compress and decompress audio and video files
  • 273.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 273 6.8 Creating a Raw Folder for Music Files • Music files are typically stored in a new resource directory called raw − A subfolder created in the res folder • The raw resource directory must be created before music files are added to the project • In Android Studio, right-click the res folder and select New, then Android resource directory to add the raw folder
  • 274.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 274 6.9 Playing Music with a MediaPlayer Method (1 of 2) • A MediaPlayer class provides the methods to control audio playback on Android devices − Provides access to built-in MediaPlayer services for playing audio and video files • For the Aloha Music app, two MediaPlayer class variables are declared and an instance of the MediaPlayer class is assigned to each
  • 275.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 275 6.9 Playing Music with a MediaPlayer Method (2 of 2) Figure 6-26 MediaPlayer instance statements
  • 276.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 276 6.10 Starting and Resuming Music Playback (1 of 2) • The playback state of the MediaPlayer determines whether an audio file is playing − start() begins media playback − pause() pauses media playback − stop() stops media playback • Android does not have a method for determining the current state of the MediaPlayer • For the Aloha Music app, an integer variable named playing is used to track the state − A Switch decision structure changes the value of playing each time the user taps the playback/pause button
  • 277.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 277 6.10 Starting and Resuming Music Playback (2 of 2) Figure 6-27 Variable named playing is set to 0
  • 278.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 278 6.11 Changing the Text Attribute of a Component (1 of 2) • When a Button component is used as a toggle button, its text should be changed to correspond with its functionality when it is tapped • You can change the text attribute using Java code by calling the setText() method: button1.setText("Pause Ukulele Song"); • For the Aloha Music app, this method is added to the case statements of each Button component's Switch decision structure
  • 279.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 279 6.11 Changing the Text Attribute of a Component (2 of 2) Figure 6-28 The setText( ) method changes the button text in both case statements
  • 280.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 280 6.12 Changing the Visibility of a Component (1 of 4) • Visibility attribute: the Java attribute that determines whether a component is displayed on the emulator − Set to be visible by default − Code to hide a component: button2.setVisibility(View.INVISIBLE); − Code to display a component: button2.setVisibility(View.VISIBLE); • For the Aloha Music app, calls to setVisibility() are added to the Switch decision structure to make the opposing play button disappear
  • 281.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 281 6.12 Changing the Visibility of a Component (2 of 4) Figure 6-30 The setVisibility() method changes the visibility of the Button component
  • 282.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 282 6.12 Changing the Visibility of a Component (3 of 4) Figure 6-31 Completed code for MainActivity.java
  • 283.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 283 6.12 Changing the Visibility of a Component (4 of 4) • Running and Testing the Application − Click Run  Run on the Android Studio menu bar to save and test the application in the emulator − The splash screen should display for five seconds − Then the activity_main layout screen should open − Test both buttons to ensure they operate as expected
  • 284.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 284 Activity 6.2: Knowledge Check 1. Which kind of variable is visible to all methods within a class? 2. What goes in the raw folder? 3. Why do we need Switch statements in the Aloha Music app? 4. What are the two possible values for the visibility attribute?
  • 285.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 285 Activity 6.2: Knowledge Check Answers 1. Which kind of variable is visible to all methods within a class? Answer: global variable or class variable 2. What goes in the raw folder? Answer: audio or video files 3. Why do we need Switch statements in the Aloha Music app? Answer: Using Switch statements for the Button components allows us to easily perform the set of actions appropriate for the current state of the MediaPlayer (the case) when a button is tapped. 4. What are the two possible values for the visibility attribute? Answer: visible and invisible
  • 286.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 286 Self-Assessment 1. As an app consumer, have you noticed splash screens and/or background music in apps that you frequently use? If so, what do you think of these features? 2. To what extent, and in what contexts, do you anticipate using the TimerTask, Timer, and MediaPlayer classes in apps that you develop professionally? 3. Attempt to map the user flow of one of your favorite apps to the Android life cycle depicted in Figure 6-14. How easy or difficult is it to recognize the app's activities and follow their state changes?
  • 287.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 287 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 288.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 288 288 Android Bootcamp for Developers Using Java, 4e Chapter 7: Reveal! Displaying Pictures in a GridView Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 289.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 289 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 07.01 Create an Android project using a GridView component • 07.02 Add a GridView to display a two-dimensional grid of images • 07.03 Add the ImageView component in XML code • 07.04 Create an array for images • 07.05 Instantiate the GridView and ImageView components • 07.06 Use a setAdapter with an ImageAdapter Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 290.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 290 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 07.07 Code the OnItemClickListener • 07.08 Code a custom toast notification • 07.09 Display the setImageResource image • 07.10 Define a Context resource • 07.11 Determine the length of an array • 07.12 Code the getView() method Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 291.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 291 7.1 Creating an Android Project Using a GridView Component (1 of 3) • The GridView layout tool shows items in a two-dimensional scrolling grid • Endangered Species app − Users can scroll an image grid by flicking their fingers across a horizontal list of thumbnail-sized pictures − To view a larger image of one of the endangered animals, users can tap a thumbnail − A toast notification is used to display the species number for the selected image
  • 292.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 292 7.1 Creating an Android Project Using a GridView Component (2 of 3) Figure 7-2 Panda image selected in the grid
  • 293.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 293 7.1 Creating an Android Project Using a GridView Component (3 of 3) • To create this app, you must: − Add a GridView component and an ImageView component to the emulator − Update the XML code for an ImageView component not linked to a particular image − Place six images in a drawable folder − Define an array to hold the image files − Instantiate the GridView and ImageView components − Create an ImageAdapter class − Display a custom toast message − Display the selected image − Customize the ImageAdapter class − Define the layout using the getView() method
  • 294.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 294 7.2 Adding a GridView to Display a Two- Dimensional Grid of Images (1 of 4) • A View container is a rectangular area of the screen that displays an image or text object − Can include layouts such as GridView, RadioGroup, ScrollView, TabHost, and ListView • The GridView container displays a horizontal list of objects − This layout can be used to build applications that show elements in a table or grid − Photos in a grid can be sized as thumbnail images or full-screen images − Photos can be stored in the drawable folders, in a phone’s storage, or on a website such as Picasa
  • 295.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 295 7.2 Adding a GridView to Display a Two- Dimensional Grid of Images (2 of 4) GridView Attribute Name Description android:columnWidth Specifies the fixed width for each column of the grid android:numColumns Defines the number of columns in the grid android:horizontalSpacing Defines the default horizontal spacing between columns in the grid android:verticalSpacing Defines the default vertical spacing between rows in the grid
  • 296.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 296 7.2 Adding a GridView to Display a Two- Dimensional Grid of Images (3 of 4) Figure 7-4 GridView component in the emulator for the Endangered Species project
  • 297.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 297 7.2 Adding a GridView to Display a Two- Dimensional Grid of Images (4 of 4) Figure 7-6 Customized GridView XML code
  • 298.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 298 7.3 Adding the ImageView Component in XML Code (1 of 4) • Typically, you add an ImageView component by dragging it onto the emulator • If an image should appear in the ImageView component only after a user event, you can prevent an image from being automatically assigned to the component by entering the XML code for it instead − The component is coded in activity_main.xml − The source display is coded in MainActivity.java • The ImageView's content description is entered into the String table • The image files for display are placed in the drawable folder
  • 299.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 299 7.3 Adding the ImageView Component in XML Code (2 of 4) Figure 7-7 ImageView XML code
  • 300.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 300 7.3 Adding the ImageView Component in XML Code (3 of 4) Figure 7-8 Adding a String value
  • 301.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 301 7.3 Adding the ImageView Component in XML Code (4 of 4) Figure 7-9 Images copied
  • 302.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 302 7.4 Creating an Array for Images (1 of 2) • The images for the GridView must be referenced in the code and assigned to an array • Arrays provide access to data by using a numeric index (subscript) to identify each element • Syntax: Integer[] Animals = {R.drawable.elephant, R.drawable.gorilla, R.drawable.leopard, R.drawable.panda, R.drawable.polar, R.drawable.rhino}; Element of Array Image File Animals[0] elephant.png Animals[1] gorilla.png Animals[2] leopard.png Animals[3] panda.png Animals[4] polar.png Animals[5] rhino.png
  • 303.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 303 7.4 Creating an Array for Images (2 of 2) Figure 7-11 ImageView referenced
  • 304.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 304 7.5 Instantiating the GridView and ImageView Components • The code to instantiate the GridView assigns the name gvAnimals to the component created in activity_main.xml • Java code syntax for instantiating the GridView: GridView grid = findViewById(R.id.gvAnimals); • Java code syntax for instantiating the ImageView, which is assigned as a class variable: final ImageView pic = findViewById(R.id.ivLarge);
  • 305.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 305 7.6 Using a setAdapter with an ImageAdapter (1 of 3) • A setAdapter provides a data model for the GridView layout − This adapter helps to set the data from an array list or a database to the items of the grid • Java code syntax to instantiate a custom BaseAdapter class called ImageAdapter and apply it to the GridView using setAdapter(): grid.setAdapter(new ImageAdapter());
  • 306.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 306 7.6 Using a setAdapter with an ImageAdapter (2 of 3) Figure 7-14 Instance of the ImageAdapter class
  • 307.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 307 7.6 Using a setAdapter with an ImageAdapter (3 of 3) Figure 7-15 ImageAdapter class and auto-generated code
  • 308.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 308 Activity 7.1: Knowledge Check 1. What is the name of the component that displays a horizontal list of objects? 2. What is a View container? 3. What is each item in an array called? 4. Where should photos you use in an Android app project be stored? 5. The data model for a GridView layout is provided by a(n) _____.
  • 309.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 309 Activity 7.1: Knowledge Check Answers 1. What is the name of the component that displays a horizontal list of objects? Answer: GridView component 2. What is a View container? Answer: A rectangular area of the screen that displays an image or text object. 3. What is each item in an array called? Answer: element 4. Where should photos you use in an Android app project be stored? Answer: drawable folder 5. The data model for a GridView layout is provided by a(n) _____. Answer: setAdapter
  • 310.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 310 7.7 Coding the OnItemClickListener (1 of 4) • The OnItemClickListener awaits user interaction within the GridView component • The onItemClick() method is executed by the OnItemClickListener when the user touches an image within the GridView component − Has four arguments: parent, view, position, and id • ListView and GridView components enable the Android device to monitor for click events
  • 311.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 311 7.7 Coding the OnItemClickListener (2 of 4) • Java syntax for the onItemClick() method: grid.setOnItemClickListener(new OnItemClickListener( ) { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }}; }
  • 312.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 312 7.7 Coding the OnItemClickListener (3 of 4) Argument Purpose AdapterView<?> parent The AdapterView records “where” the user actually touched the screen in the argument variable parent. In other words, if the app has more than one View component, the AdapterView determines if the user touched the GridView component or another component in the application. View view The View parameter is the specific View within the item that the user touched. This is the View provided by the adapter. int position This is one of the most important portions of this statement in the chapter project. The position argument is an integer value that holds the position of the View in the adapter. For example, if the user taps the elephant picture, the integer value of 1 is stored in position because the elephant picture is the second image in the Animals array. long id The GridView component is displayed across multiple rows of the Android device. The argument id determines the row id of the item that was selected by the user. This is especially useful for a GridView component that has multiple rows in the layout.
  • 313.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 313 7.7 Coding the OnItemClickListener (4 of 4) Figure 7-16 GridView OnItemClickListener
  • 314.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 314 7.8 Coding a Custom Toast Notification (1 of 2) • Toast notifications display a temporary message to the user • Toast notification code used in earlier chapters: Toast.makeText(MainActivity.this, "Typical Toast Message", Toast.LENGTH_SHORT).show(); • When you use a toast notification in an onItemClick() method rather than directly in the MainActivity, the reference to MainActivity.this creates an error • In Android programs, you can place the getBaseContext() method in another method (such as onItemClick()) that is triggered only when the user touches the GridView component • If you do, the getBaseContext() method obtains a Context instance
  • 315.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 315 7.8 Coding a Custom Toast Notification (2 of 2) • Figure 7-17 Toast message displayed when the user selects the leopard image • Notice that even though the leopard is in position Animals[2] in the Animals array, the custom toast message states “Selected Species 3” − Array position 2 is really the third image because the array values begin with 0 • Java syntax for this toast notification: Toast.makeText(getBaseContext(), "Selected Species " + (position + 1), Toast.LENGTH_SHORT).show();
  • 316.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 316 7.9 Displaying the setImageResource Image (1 of 2) • When the user selects an animal picture, a toast message appears with an ImageView component that displays the selected image • An ImageView component is defined either by the android:src attribute in the XML element or by the setImageResource(int) method • Java syntax for the setImageResource() method: pic.setImageResource(Animals[position]);
  • 317.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 317 7.9 Displaying the setImageResource Image (2 of 2) • Customizing the ImageAdapter Class − The ImageAdapter class was called with this line of code: gr.setAdapter(new ImageAdapter (this)); − The ImageAdapter class determines the layout of the GridView − The context and images of the GridView need to be referenced within the ImageAdapter class − The tasks to complete inside the ImageAdapter class:  Manage the layout of the GridView  Connect the data sources from the array for display within the GridView component
  • 318.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 318 7.10 Defining the Context of the ImageAdapter Class (1 of 3) • A Context variable is used to load and access resources for the application • Constructors are used to initialize the instance variables of an object − A constructor is similar to a method but always has the same name as the class whose instance members it initializes − A constructor does not contain a return type • When we call a constructor, we often have to pass a Context, and we often use the Java command "this" to pass the activity Context to the application Context
  • 319.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 319 7.10 Defining the Context of the ImageAdapter Class (2 of 3) • Java code that initializes the class variable named Context so it can hold each image in the GridView temporarily before it is displayed: private Context context; public ImageAdapter(Context c){ context=c; }
  • 320.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 320 7.10 Defining the Context of the ImageAdapter Class (3 of 3) Figure 7-22 Passing the Context to the ImageAdapter Class
  • 321.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 321 7.11 Calculating the Length of an Array • The length of an array is determined by the number of elements in the array • When the ImageAdapter class is called, the getCount() method determines how many pictures should be displayed in the GridView − getCount() calls length(), which returns the number of pictures in the array: public int getCount() { return Animals.length; } − The Java length() method returns an integer value for any string or array • A Java method is a series of statements that perform some repeated task
  • 322.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 322 7.12 Coding the getView() Method (1 of 6) • The getView() method uses a Context to create a new ImageView instance that temporarily holds each image displayed in the GridView • The returned pic is a scaled, resized image, ready to display in the GridView component • getView() method Java code: public View getView(int position, View convertView, ViewGroup parent){ pic = new ImageView(context); pic.setImageResource(Animals[position]); pic.setScaleType(ImageView.ScaleType.FIT_XY); pic.setLayoutParams(new GridView.LayoutParams(330,300)); return pic; }
  • 323.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 323 7.12 Coding the getView() Method (2 of 6) • Scaling keeps or changes the aspect ratio of the image within the ImageView component ScaleType Option Meaning ImageView.ScaleType.CENTER This option centers the image within the View type but does not change the aspect ratio (no scaling). ImageView.ScaleType.CENTER_CROP This option centers the image within the View type and scales the image uniformly, maintaining the same aspect ratio. ImageView.ScaleType.FIT_XY This option scales the image to fit the View type. The aspect ratio is changed to fit within the component.
  • 324.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 324 7.12 Coding the getView() Method (3 of 6) • After the image is scaled, the GridView images are resized to fit the custom layout • LayoutParams are set to GridView.LayoutParams(330,300) − 330 pixels across the image − Height of 300 pixels • The last statement in the getView() method, return pic;, must return the instance of the ImageView component named pic to display in the GridView component • Running and Testing the Application − Save the app, run it in the emulator, and test its functionality
  • 325.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 325 7.12 Coding the getView() Method (4 of 6) Figure 7-28 Complete code of MainActivity.java (part 1)
  • 326.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 326 7.12 Coding the getView() Method (5 of 6) Figure 7-28 Complete code of MainActivity.java (part 2)
  • 327.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 327 7.12 Coding the getView() Method (6 of 6) Figure 7-28 Complete code of MainActivity.java (part 3)
  • 328.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 328 Activity 7.2: Knowledge Check 1. What do you use to initialize the instance variables of an object? 2. A series of Java statements that perform some repeated task is called a(n) _____. 3. What method does the OnItemClickListener execute when the user touches its associated component? 4. What change to the Java code for a typical toast notification must you make to use a toast message within an onItemClick() method? 5. What does the getView() method do?
  • 329.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 329 Activity 7.2: Knowledge Check Answers (1 of 2) 1. What do you use to initialize the instance variables of an object? Answer: constructor 2. A series of Java statements that perform some repeated task is called a(n) _____. Answer: method 3. What method does the OnItemClickListener execute when the user touches its associated component? Answer: onItemClick()
  • 330.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 330 Activity 7.2: Knowledge Check Answers (2 of 2) 4. What change to the Java code for a typical toast notification must you make to use a toast message within an onItemClick() method? Answer: You must replace MainActivity.this with a Context class called getBaseContext(). 5. What does the getView() method do? Answer: It uses a Context to create a new ImageView instance that temporarily holds each image displayed in the GridView.
  • 331.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 331 Self-Assessment 1. How do you anticipate using the GridView component (or similar Android layout tools) in your own apps? Did the use of this component in the Endangered Species project give you any inspiration for your own app projects? 2. Do you enjoy creating custom classes such as the ImageAdapter used in the Endangered Species app? Or would you prefer to stick to classes provided in the Android libraries? Explain the reasons for your preference.
  • 332.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 332 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 333.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 333 333 Android Bootcamp for Developers Using Java, 4e Chapter 8: Design! Using a DatePicker on a Tablet Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 334.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 334 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 08.01 Create an Android app on a tablet • 08.02 Design a tablet app with modern design principles • 08.03 Add an Android virtual device for the tablet • 08.04 Design a tablet table layout • 08.05 Add date, time, and clock components Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 335.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 335 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 08.06 Create a Calendar class • 08.07 Select the date from the DatePickerDialog • 08.08 Add the onDateSet() method • 08.09 Display the date using the getTime() method • 08.10 Add an Android theme Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 336.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 336 Introduction • Android Apps not limited to phones • Tablets are used to: − Connect to the Internet − Play games − Use social media − Check e-mail − And more
  • 337.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 337 8.1 Creating an Android App on a Tablet (1 of 2) • Figure 8-2 DatePicker calendar component in a dialog box • Figure 8-3 TextView component displays reservation
  • 338.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 338 8.1 Creating an Android App on a Tablet (2 of 2) • Steps required to create the Sailing Adventures application: 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 TextView components and the Button component 6. Initialize a DatePickerDialog with the current date and listen for the user to select a date 7. Return the selected date 8. Display the selected reservation date in the TextView component
  • 339.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 339 8.2 Designing a Tablet App with Modern Design Principles (1 of 2) • 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) • Native Android tablet apps create an optimal user experience based on the most common tablet screen sizes − Between 7.3 and 10.1 inches (diagonally) − 2560 × 1600 pixel resolution and 16:10 screen ratio − iPad Air specs: 9.7 inches, 2048 × 1536 pixels, 4:3 screen ratio
  • 340.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 340 8.2 Designing a Tablet App with Modern Design Principles (2 of 2) • Design Best Practices for Tablets − Understand the use case scenarios for tablets − Keep screens uncluttered and use large-enough components − Make the design simple and 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 resources for screen densities (DPI) − Create a unique experience for phone and tablet designs − Use larger fonts than with a phone app
  • 341.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 341 8.3 Adding an Android Virtual Device for the Tablet (1 of 5) • An API platform and Android virtual devices (ADVs) are necessary for developing tablet apps • The minimum required SDK should be set to API 13: Android 3.2 (Honeycomb) to cover the oldest Android tablets • The target SDK is automatically set to cover the most recent versions of Android tablets • Each Android device configuration is stored in an AVD • First steps to create the Sailing Adventures app: create the new project and add the appropriate AVD configuration
  • 342.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 342 8.3 Adding an Android Virtual Device for the Tablet (2 of 5) Figure 8-5 New Project window
  • 343.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 343 8.3 Adding an Android Virtual Device for the Tablet (3 of 5) Figure 8-6 The activity_main.xml file is displayed to select a tablet device
  • 344.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 344 8.3 Adding an Android Virtual Device for the Tablet (4 of 5) Figure 8-8 Selecting a Nexus 10 virtual device
  • 345.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 345 8.3 Adding an Android Virtual Device for the Tablet (5 of 5) • Creating the String Table − The sailboat image for the Sailing Adventures app must be added to the drawable folder − The text values for the components must be entered into the strings.xml file
  • 346.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 346 8.4 Designing a Tablet Table Layout (1 of 7) • The LinearLayout and TableLayout elements can be combined to create a simple, clean interface for a table app • TableLayout is composed of TableRow components—one for each row • You add a view to a row to create additional columns sail.png Title Day trip description Reservation button Reservation date after selection
  • 347.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 347 8.4 Designing a Tablet Table Layout (2 of 7) • The padding property can be used to offset the content of the component by a specific number of pixels • The typeface property sets the style of the text to a font family such as monospace, sans serif, or serif • The Sailing Adventures app's TableLayout includes four rows: − Two stacked TextView components − One Button component − One more TextView component
  • 348.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 348 8.4 Designing a Tablet Table Layout (3 of 7) • XML syntax for the app's structure: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <ImageView /> <TableLayout <TableRow> <TextView /> </TableRow> <TableRow> <TextView /> </TableRow> <TableRow> <Button /> </TableRow> <TableRow> <TextView /> </TableRow> </TableLayout> </LinearLayout>
  • 349.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 349 8.4 Designing a Tablet Table Layout (4 of 7) Figure 8-13 LinearLayout in XML code
  • 350.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 350 8.4 Designing a Tablet Table Layout (5 of 7) Figure 8-14 ImageView component in XML code
  • 351.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 351 8.4 Designing a Tablet Table Layout (6 of 7) Figure 8-15 TableLayout XML code for first two TableRows
  • 352.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 352 8.4 Designing a Tablet Table Layout (7 of 7) Figure 8-16 TableLayout XML code for last two TableRows
  • 353.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 353 Activity 8.1: Knowledge Check 1. What is the difference between native applications and emulated applications? 2. What is the name of the first Android operating system that was designed to support tablets? 3. What property is used to offset the content of a component by a specific number of pixels? 4. How are rows and columns created in an app with a TableLayout?
  • 354.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 354 Activity 8.1: Knowledge Check Answers 1. What is the difference between native applications and emulated applications? Answer: Native applications are installed locally on a platform whereas emulated applications are converted in real time through a browser. 2. What is the name of the first Android operating system that was designed to support tablets? Answer: Android Honeycomb 3.2 (API 13) 3. What property is used to offset the content of a component by a specific number of pixels? Answer: padding 4. How are rows and columns created in an app with a TableLayout? Answer: Rows are created by adding TableRow XML elements, and columns are added by adding views to the TableRow elements.
  • 355.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 355 8.5 Using Date, Time, and Clock Components (1 of 3) • Android provides ready-to-use components called dialogs or dialog boxes that enable the user to pick a valid time or date • A DatePicker component is displayed in a dialog box to help the user determine and choose a valid date − Similar components: TimePicker, CalendarView, AnalogClock • All Android devices keep a numeric representation of the system’s current date and time, which can be displayed in multiple formats • Creating a component to enter the date is crucial because requiring users to type the date in a text box can lead to multiple errors, including incorrect formats or typos
  • 356.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 356 8.5 Using Date, Time, and Clock Components (2 of 3) • Instantiating the Components − The tvReservation TextView component is referenced in multiple methods, so this instantiation must be declared as a class variable − The TextView component, Button component, and Button OnClickListener must all be instantiated
  • 357.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 357 8.5 Using Date, Time, and Clock Components (3 of 3) Figure 8-20 The onClickListener() method for the button
  • 358.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 358 8.6 Creating a Calendar Class (1 of 4) • The Android system date is accessed by using the Calendar class, which converts information between a Date object and a set of integer fields such as YEAR, MONTH, and DAY_OF_MONTH • The getInstance() method returns a calendar date or time based on the system settings • The date constants in this class are YEAR, MONTH, and DAY_OF_MONTH − DAY_OF_YEAR displays the day number of the current year • Syntax for creating an instance of the Calendar class named c: Calendar c = Calendar.getInstance();
  • 359.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 359 8.6 Creating a Calendar Class (2 of 4) • Date Format − The DateFormat Java class formats the date into a String value − Syntax: DateFormat fmtDate = DateFormat.getDateInstance(); • DatePickerDialog Input − The DatePickerDialog allows you to select a date from a DatePicker View  Other dialog boxes include the TimePickerDialog and ProgressDialog − The field manipulation method called get accesses the system date or time − set changes the current date or time
  • 360.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 360 8.6 Creating a Calendar Class (3 of 4) • DatePickerDialog Input (continued) − Syntax for launching the DatePickerDialog in the onClick() method and passing it the values for the current year, month, and day: public void onClick(View v) { // TODO Auto-generated method stub new DatePickerDialog(MainActivity.this, d, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show(); } − The variable d is assigned later to the user-selected date
  • 361.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 361 8.6 Creating a Calendar Class (4 of 4) Figure 8-23 DatePickerDialog launched within the onClick() method
  • 362.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 362 8.7 Selecting the Date from the DatePickerDialog • To enable access to the system date, the variables are initialized and displayed for the current YEAR, MONTH, and DAY_OF_MONTH • Next, the DatePickerDialog component must await user interaction via an OnDateSetListener named d, which listens for a callback indicating that the user has filled in the reservation date • Syntax for creating the onDateSetListener: DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { }
  • 363.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 363 Activity 8.2: Knowledge Check 1. What are some time and date components that are available in Java? 2. A(n) _____ listens for a callback indicating that the user has filled in a date in a DatePickerDialog. 3. Calling _____ on a Calendar instance accesses the device system date’s month. 4. Why is using a component designed specifically to allow a user to enter a date preferable to using a text box? 5. What class is responsible for converting information between a Date object and a set of integer fields such as YEAR, MONTH, and DAY_OF_MONTH?
  • 364.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 364 Activity 8.2: Knowledge Check Answers 1. What are some time and date components that are available in Java? Answer: TimePicker, DatePicker, CalendarView, Chronometer, and AnalogClock 2. A(n) _____ listens for a callback indicating that the user has filled in a date in a DatePickerDialog. Answer: OnDateSetListener 3. Calling _____ on a Calendar instance accesses the device system date’s month. Answer: get(Calendar.MONTH) 4. Why is using a component designed specifically to allow a user to enter a date preferable to using a text box? Answer: A calendar component ensures the user’s entry is a valid date. 5. What class is responsible for converting information between a Date object and a set of integer fields such as YEAR, MONTH, and DAY_OF_MONTH? Answer: Calendar class
  • 365.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 365 8.8 Adding the onDateSet() Method • When the user selects a date from the DatePickerDialog, the onDateSet() method automatically obtains the date selected by the user • Syntax for the onDateSet() method: public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, dayOfMonth); }
  • 366.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 366 8.9 Displaying the Date Using the getTime() Method (1 of 3) • The getTime() method returns the time value in the Date object • Syntax for displaying the user-selected date in the TextView object using the getTime() method: reservation.setText("Your reservation is set for " + Date.format(c.getTime( )));
  • 367.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 367 8.9 Displaying the Date Using the getTime() Method (2 of 3) Figure 8-26 Completed Java code for Sailing Adventures app, Part 1
  • 368.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 368 8.9 Displaying the Date Using the getTime() Method (3 of 3) Figure 8-26 Completed Java code for Sailing Adventures app, Part 2
  • 369.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 369 8.10 Adding an Android Theme (1 of 4) • A theme is a style applied to an Activity or an entire application • Themes are Android’s mechanism for applying a consistent style to an app or Activity on any device • The style specifies the visual properties of the elements that make up a user interface, such as color, height, padding, and font size • Themes alter features such as the background wallpaper and may be used to display (or not) a title bar or action bar • Every UI element in the application should follow the app's parent theme
  • 370.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 370 8.10 Adding an Android Theme (2 of 4) Figure 8-28 Android theme examples
  • 371.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 371 8.10 Adding an Android Theme (3 of 4) • Changing a Theme: You must define themes in the themes.xml or styles.xml file in the values subfolder • Running and Testing the Application: Save and test the application in the tablet emulator Theme Code in themes.xml Description <style name="AppTheme" parent="Base.Theme.AppCompat"> Black background, gray title bar <style name="AppTheme" parent="Base.Theme.AppCompat.Light"> White background, gray title bar <style name="AppTheme" parent="Theme.Design.NoActionBar"> White background, no title bar
  • 372.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 372 8.10 Adding an Android Theme (4 of 4) Figure 8-29 The themes.xml file
  • 373.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 373 Self-Assessment 1. Which of the best practices for tablet design listed in Chapter 8 do you think are most important to ensuring a good user experience? Did any of these guidelines surprise you? 2. How do you typically keep up to date with best practices for design in your own software development? Consider how you might further improve your skills in this area. 3. Have you used any of the Java classes/methods for handling dates and times discussed in this chapter in other projects? If so, compare their application in your project(s) with the way they are used in the Sailing Adventures app.
  • 374.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 374 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 375.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 375 375 Android Bootcamp for Developers Using Java, 4e Chapter 9: Customize! Navigating with a Primary/Detail Flow Activity on a Tablet Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 376.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 376 Chapter Objectives When you complete this chapter, you will be able to: • 09.01 Describe responsive design for Android apps • 09.02 Create an Android tablet project using an application template • 09.03 Use the Primary/Detail Flow template • 09.04 Modify the Primary/Detail Flow template • 09.05 Design an XML TableLayout • 09.06 Add a WebView component • 09.07 Customize the content of the sample template file • 09.08 Display a custom layout in the detail pane Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 377.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 377 9.1 Understanding Responsive Design (1 of 5) • Design elements such as fluid grids and flexible images can adapt to various screen sizes • The Bike and Barge application project relies on built-in templates that use the best presentation mode based on the size of the device − Multipane tablet interface − Items from a list in the left pane can be tapped to make the selected content display in the right pane
  • 378.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 378 9.1 Understanding Responsive Design (2 of 5) Figure 9-2 Selecting Photos in the left pane
  • 379.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 379 9.1 Understanding Responsive Design (3 of 5) • Steps to create the Bike and Barge app: − Create an application with a Primary/Detail Flow template − Add the images to the drawable folder − Add text to the String table − Create the photos.xml file using a TableLayout for the detail pane of the first list item − Create the tour.xml file for the detail pane of the second list item − Change the default TextView component to a WebView component
  • 380.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 380 9.1 Understanding Responsive Design (4 of 5) • Steps to create the Bike and Barge app (continued): − Update the AndroidManifest file to include an Internet permission − Customize the PlaceholderContent class to display the item list − Customize the PlaceholderContent class to connect the app to the website − Modify the ItemDetailFragment.java class to:  Display photos.xml in the detail pane  Display tour.xml in the detail pane  Display a website in a browser
  • 381.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 381 9.1 Understanding Responsive Design (5 of 5) • Creating Apps with Responsive Design − Responsive design: an approach to designing apps and websites that provides an optimal viewing experience across as many devices as possible − Android Studio provides responsive design templates, which allow you to build an app once but display it on multiple devices − Thanks to the use of one of these templates, the Bike and Barge app can be run on a smartphone emulator without code changes  There, the app runs as two Activities instead of two panes
  • 382.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 382 9.2 Creating an Android Tablet Project Using an Application Template (1 of 2) • Application templates are used to create basic Android applications that can immediately be run and tested on an Android device of any size • Android templates are available when you create a new Android project − The Bike and Barge app uses the Primary/Detail Flow application template − A similar template is the Navigation Drawer Activity
  • 383.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 383 9.2 Creating an Android Tablet Project Using an Application Template (2 of 2) Figure 9-6 Android application templates
  • 384.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 384 9.3 Using the Primary/Detail Flow Template (1 of 2) • The Primary/Detail Flow template creates an adaptive, responsive layout for a set of list items and associated detail content − An interface design concept in which a list of items (the Primary list) appears in a narrow vertical pane along the left edge of the screen − The item details are displayed on the right side of the tablet screen in the wider detail pane − Referred to as a two-pane layout • To begin creating the Bike and Barge app, you start a new project and select the Primary/Detail Flow template
  • 385.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 385 9.3 Using the Primary/Detail Flow Template (2 of 2) Figure 9-9 Creating the Bike and Barge Android app
  • 386.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 386 9.4 Modifying the Structure of the Primary/Detail Flow Template (1 of 3) Figure 9-10 Template code files in the java folder
  • 387.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 387 9.4 Modifying the Structure of the Primary/Detail Flow Template (2 of 3) • Java Files − ItemDetailFragment.java  Displays the fragment_item_detail.xml layout file  Can be customized to determine which detailed items to display − ItemDetailHostActivity.java: displays the activity_item_detail.xml layout file if a smartphone is detected − ItemListFragment.java: displays the activity_item_list.xml layout file
  • 388.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 388 9.4 Modifying the Structure of the Primary/Detail Flow Template (3 of 3) • Layout Files − activity_item_detail.xml: used by the app to display the FrameLayout instance when a smartphone is detected − fragment_item_detail.xml: used by the app when a smartphone is detected to display the Primary list fragment − fragment_item_detail.xml (sw600dp): used when a tablet is detected; the app is displayed in a two-pane layout containing both the Primary item list fragment and the item detail container − item_list_content.xml: displays the detail pane using the onCreateView() method when a smartphone or a tablet is detected
  • 389.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 389 9.5 Designing an XML TableLayout (1 of 6) • Before designing the XML layouts, first place the three required images in the drawable folder and then reference them in the photos.xml layout • The String table is responsible for the text displayed in the app − The template's String table has two initial strings:  The first string is displayed in the title bar of the tablet  The second string is displayed in the title bar of a smaller device such as a smartphone − Other strings required for the app can be added to this table • photos.xml uses a TableLayout consisting of three rows
  • 390.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 390 9.5 Designing an XML TableLayout (2 of 6) Figure 9-12 String table for the Bike and Barge app
  • 391.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 391 9.5 Designing an XML TableLayout (3 of 6) Figure 9-15 First row of the TableLayout
  • 392.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 392 9.5 Designing an XML TableLayout (4 of 6) Figure 9-16 Second and third rows of the TableLayout
  • 393.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 393 9.5 Designing an XML TableLayout (5 of 6) Figure 9-17 Completed TableLayout in photos.xml in Design view
  • 394.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 394 9.5 Designing an XML TableLayout (6 of 6) • Creating a TextView XML Layout for the Second List Item − The second list item of the Bike and Barge app contains two TextView components within a LinearLayout − First TextView component XML code (the second is very similar): <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvTitle" android:textSize="50sp" android:paddingStart="50sp" android:paddingBottom="60sp" />
  • 395.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 395 9.6 Creating a WebView XML Layout (1 of 2) • Webview object: a View component that displays webpages • Permission: a restriction that limits access to a part of the code or to data on the device • Code syntax for requesting permission to connect to the Internet within the AndroidManifest.xml file: <uses-permission android:name="android.permission.INTERNET" /> • Code syntax for enabling cleartext (disabled by default): android:usesCleartextTraffic="true"
  • 396.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 396 9.6 Creating a WebView XML Layout (2 of 2) • The TextView component code must be modified to replace it with a WebView component in the fragment_item_detail.xml layout file (sw600dp and regular versions) • WebView component syntax: <WebView android:id="@+id/item_detail" android:layout_width="match_parent" android:layout_height="match_parent" tools:context= ".ItemDetailFragment" />
  • 397.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 397 Activity 9.1: Knowledge Check 1. What does Android Studio do when you create a project using the Primary/Detail Flow template? 2. What kind of template files are “activity_item_detail,” “fragment_item_detail,” and “item_list_content”? 3. What code do you include in the AndroidManifest.xml file to give permission to an app to access the Internet? 4. To provide an optimal viewing experience across as many devices as possible for your Android application, you should implement _____ principles.
  • 398.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 398 Activity 9.1: Knowledge Check Answers (1 of 2) 1. What does Android Studio do when you create a project using the Primary/Detail Flow template? Answer: Studio adds a group of Java and XML layout resource files, each with a specific purpose, to the app project automatically. 2. What kind of template files are “activity_item_detail,” “fragment_item_detail,” and “item_list_content”? Answer: XML layout files
  • 399.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 399 Activity 9.1: Knowledge Check Answers (2 of 2) 3. What code do you include in the AndroidManifest.xml file to give permission to an app to access the Internet? Answer: <uses-permission android:name="android.permission.INTERNET" /> 4. To provide an optimal viewing experience across as many devices as possible for your Android application, you should implement _____ principles. Answer: responsive design
  • 400.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 400 9.7 Customizing the Content of the Sample Template File • The Java class file PlaceholderContent.java (in the placeholder folder) provides sample content to display in the UI of the template • This class can be customized or replaced as needed • Code syntax for using the addItem command to display list items in the left pane of the tablet display/first Activity on a smartphone: // Add some sample items. addItem(new PlaceholderItem("1", "Photos", "")); addItem(new PlaceholderItem("2", "Tour", "")); addItem(new PlaceholderItem("3", "Website", "https://bikebarge.com"));
  • 401.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 401 9.8 Displaying the Custom Layout in the Detail Pane (1 of 6) • A fragment is a piece of an application’s user interface or behavior that can be placed in an Activity − Fragments are essentially a sub-Activity hosted inside another Activity • To determine if two String objects match exactly: − Use the .equals() method, not the == operator − The == operator compares if two objects are exactly the same object − Two strings may be different objects but have the same exact characters − The .equals() method is used to compare strings for equality
  • 402.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 402 9.8 Displaying the Custom Layout in the Detail Pane (2 of 6) • Java syntax used to compare the value of a string: if (mItem != null) { if (mItem.id.equals("1")) } • Code within ItemDetailFragment.java to display a custom XML layout file named photos.xml in the detail pane: rootView = inflater.inflate(R.layout.photos, container, false); − Three arguments: The first part displays the XML layout, the second part applies the layout parameters to the container, and the third part is false, a Boolean type declaring that the layout was already passed to the container
  • 403.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 403 9.8 Displaying the Custom Layout in the Detail Pane (3 of 6) • Syntax used to display the webpage URL with the variable named item_url within the WebView component: ((WebView) rootView.findViewById(R.id.item_detail)).loadUrl(mItem.cont ent); • We want the page to load in the right pane, not in a separate screen in the Chrome browser (the default), so we create an instance of the WebViewClient class and assign it the shouldOverrideUrlLoading() callback method • We code four conditional If statements to display the XML layout and the webpage within the detail pane
  • 404.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 404 9.8 Displaying the Custom Layout in the Detail Pane (4 of 6)
  • 405.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 405 9.8 Displaying the Custom Layout in the Detail Pane (5 of 6) Figure 9-27 ItemDetailFragment.java class customized for the third list item
  • 406.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 406 9.8 Displaying the Custom Layout in the Detail Pane (6 of 6) • Running and Testing the Application − Click the Run app button on the Standard toolbar, select the Nexus 10 emulator to test the application, and then click the OK button − Unlock the emulator (if necessary) − Test each list item to ensure it works as intended − You must have Internet connectivity to open the webpage and enough memory available to handle the app's connection to the Web − If you have trouble with the Nexus 10 emulator, the 10.1-inch WXGA tablet emulator is an alternative
  • 407.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 407 Activity 9.2: Knowledge Check 1. What is a fragment in the context of an Android app? 2. To determine if two String objects contain the same characters, you use the _____; to determine if they are the same object, you use the _____. 3. I am a method with three arguments. The first part displays an XML layout, the second part applies the layout parameters to the container, and the third part is false, a Boolean type declaring that that layout was already passed to the container. Who am I?
  • 408.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 408 Activity 9.2: Knowledge Check Answers 1. What is a fragment in the context of an Android app? Answer: A piece of an application’s user interface or behavior that can be placed in an Activity. A fragment is essentially a sub-Activity hosted inside another Activity. 2. To determine if two String objects contain the same characters, you use the _____; to determine if they are the same object, you use the _____. Answer: .equals() method; == operator 3. I am a method with three arguments. The first part displays an XML layout, the second part applies the layout parameters to the container, and the third part is false, a Boolean type declaring that that layout was already passed to the container. Who am I? Answer: inflate() method
  • 409.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 409 Self-Assessment 1. You now have experience creating an app using an empty Activity and using a template with placeholder content. Which of these methods do you find more enjoyable overall? What aspects of each did you find most and least ergonomic? 2. Which method do you expect to employ more often as a professional developer? For which type of app projects do you anticipate using each route?
  • 410.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 410 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 411.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 411 411 Android Bootcamp for Developers Using Java, 4e Chapter 10: Move! Creating Animation Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 412.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 412 Chapter Objectives (1 of 2) When you complete this chapter, you will be able to: • 10.01 Create an Android application with Frame and Tween animation • 10.02 Create frame-by-frame animation • 10.03 Set the background resource • 10.04 Use the start() and stop() methods • 10.05 Move with Tween animation Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 413.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 413 Chapter Objectives (2 of 2) When you complete this chapter, you will be able to: • 10.06 Launch Tween animation • 10.07 Add the layout for the Tween image • 10.08 Set Tween animation attributes • 10.09 Code the startAnimation() method • 10.10 Change the orientation of the emulator Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 414.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 414 10.1 Creating an Android Application with Frame and Tween Animation (1 of 11) • Animation is everywhere − For example, in Dragon Ball Legends, Baldur's Gate, Word Link, Minecraft, and Candy Crush • A motion tween is used to animate an object and specifies − A start state − A uniform transition type − The number of times to repeat the motion
  • 415.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 415 10.1 Creating an Android Application with Frame and Tween Animation (2 of 11) Figure 10-1 Northern Lights Animation app using Frame animation Figure 10-2 Northern Lights Animation app using Tween animation
  • 416.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 416 10.1 Creating an Android Application with Frame and Tween Animation (3 of 11) • Steps to create the Northern Lights Animation app: 1. Add the layout for the image and button objects in activity_main.xml. 2. Add five images to the drawable folder. 3. Add a Frame animation XML file to the project. 4. Set the duration between frames in the frame-by-frame animation. 5. Declare and instantiate the ImageView, Button, and animationDrawable controls.
  • 417.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 417 10.1 Creating an Android Application with Frame and Tween Animation (4 of 11) • Steps to create the Northern Lights Animation app (continued): 6. Code the OnClickListeners for the Button controls. 7. Run the Frame animation application. 8. Add a Tween animation XML file to rotate the last image. 9. Create a second Activity named Tween.java to launch the rotation Tween animation with an XML layout. 10.When the application executes, change the orientation of the emulator.
  • 418.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 418 10.1 Creating an Android Application with Frame and Tween Animation (5 of 11) • Using Animation in Android − Android provides two types of animation: Frame and Tween − Frame animation (a.k.a. frame-by-frame animation) assigns a sequence of photos to play with a predefined interval between images  Like a slide show  Images rapidly replaced by new, similar images − Tween animation is created by a series of transformations on a single image  Transformations can alter position, size, rotation, and transparency
  • 419.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 419 10.1 Creating an Android Application with Frame and Tween Animation (6 of 11) • Adding the Layout for the Frame Image and Button Controls − The layout specifications reside in a ConstraintLayout in activity_main.xml − An ImageView control (ivLights) displays the Frame animation − Two button controls (btStart and btStop) start and stop the animation, respectively − A Tween animation (a second Activity) is launched when the Frame animation ends − The "Help me choose" link can be referenced when selecting the minimum SDK for a project
  • 420.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 420 10.1 Creating an Android Application with Frame and Tween Animation (7 of 11) Figure 10-3 Setting up the project in the New Project window
  • 421.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 421 10.1 Creating an Android Application with Frame and Tween Animation (8 of 11) Figure 10-4 String table
  • 422.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 422 10.1 Creating an Android Application with Frame and Tween Animation (9 of 11) Figure 10-5 ImageView XML code
  • 423.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 423 10.1 Creating an Android Application with Frame and Tween Animation (10 of 11) Figure 10-6 Two Button controls in the XML code (Part 1)
  • 424.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 424 10.1 Creating an Android Application with Frame and Tween Animation (11 of 11) Figure 10-6 Two Button controls in the XML code (Part 2)
  • 425.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 425 10.2 Creating Frame-by-Frame Animation (1 of 7) • An animation-list root element is needed to reference images stored in the drawable folders − Each item in the animation-list code specifies how many milliseconds to display the image • The opening animation-list tag includes the android:oneshot attribute − Set to true by default: the animation plays once and then stops and displays the last frame − When it is set to false, the animation plays repeatedly
  • 426.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 426 10.2 Creating Frame-by-Frame Animation (2 of 7) • Code syntax for the animation-list element: <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/lights1" android:duration= "100"/> <item android:drawable="@drawable/lights2" android:duration= "100"/> <item android:drawable="@drawable/lights3" android:duration= "100"/> <item android:drawable="@drawable/lights4" android:duration= "100"/> <item android:drawable="@drawable/lights5" android:duration= "100"/> </animation-list>
  • 427.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 427 10.2 Creating Frame-by-Frame Animation (3 of 7) • To create a Frame animation XML file, you: − Add the image files to the drawable resources folder − Add an XML file of the Animator resource type with animation-list as the root element − Nest an item element for each frame of the animation (image) within the animation-list element
  • 428.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 428 10.2 Creating Frame-by-Frame Animation (4 of 7) Figure 10-8 Creating the animation.xml file
  • 429.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 429 10.2 Creating Frame-by-Frame Animation (5 of 7) Figure 10-10 Entering the animation-list items with image names and duration
  • 430.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 430 10.2 Creating Frame-by-Frame Animation (6 of 7) • Coding the AnimationDrawable Object − The AnimationDrawable class provides the methods for drawable animations to create a sequence of frame-by-frame images played in order − In Android development, frame-based animations and image transitions are defined as drawables − The instance of AnimationDrawable is instantiated as a class variable because it is used in multiple methods within the MainActivity class
  • 431.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 431 10.2 Creating Frame-by-Frame Animation (7 of 7) Figure 10-11 Instantiating the AnimationDrawable class variable
  • 432.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 432 10.3 Setting the Background Resource (1 of 6) • The Background property of an image can be set to any full drawable resource, such as a .png file, a 9-patch image file, or a solid color • A 9-patch image has predefined “stretching” areas that maintain the same look on different screen sizes − Named for the nine areas, called patches, that scale separately • The setBackgroundResource() method places images in a frame-by-frame display that resembles a slide show − Each frame points to one of the images that were assembled in the XML resource file − The ImageView instance is the image that you want to animate; the image is set to the animation drawable as its background
  • 433.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 433 10.3 Setting the Background Resource (2 of 6) • Syntax for setting the background resource: ImageView ivFrame=findViewById(R.id.ivLights); ivFrame.setBackgroundResource(R.drawable.animation); lightsAnimation=(AnimationDrawable) ivFrame.getBackground(); − The first line instantiates the ImageView control − The second line sets the animation-list code within animation.xml as the Background property of the ivFrame ImageView − The third line assigns the instance of AnimationDrawable called lightsAnimation as the background of the five images to display in the animation
  • 434.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 434 10.3 Setting the Background Resource (3 of 6) Figure 10-14 The getBackground() method prepares the AnimationDrawable object
  • 435.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 435 10.3 Setting the Background Resource (4 of 6) • Adding Two Button Controls − The Button controls in the Northern Lights Animation app turn the Frame animation on and off − Both use a setOnClickListener to await user interaction − For the app project, you must write code to instantiate each Button control and then to add the setOnClickListener to each
  • 436.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 436 10.3 Setting the Background Resource (5 of 6) Figure 10-16 Coding the button to start the Tween animation
  • 437.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 437 10.3 Setting the Background Resource (6 of 6) Figure 10-18 OnClickListener for the second button
  • 438.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 438 Activity 10.1: Knowledge Check 1. How does Tween animation work? 2. What is an animation-list? 3. What kind of image has predefined “stretching” areas that maintain the same look on different screen sizes?
  • 439.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 439 Activity 10.1: Knowledge Check Answers 1. How does Tween animation work? Answer: Tween animation creates animation by performing a series of transformations on a single image such as changing its position, size, rotation, and transparency. 2. What is an animation-list? Answer: An element used to list, in sequence, the images that will be displayed in an animation. 3. What kind of image has predefined “stretching” areas that maintain the same look on different screen sizes? Answer: 9-patch image
  • 440.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 440 10.4 Using the start() and stop() Methods (1 of 2) • After associating AnimationDrawable with the animation images and coding the buttons, you can use the start() and stop() methods of the drawable objects to control the Frame animation − Syntax: lightsAnimation.start(); lightsAnimation.stop(); • The start() method begins the Frame animation to run continuously because oneshot is set to false • The stop() method thus must be called to stop the animation when the button that begins the Tween animation for the app is tapped
  • 441.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 441 10.4 Using the start() and stop() Methods (2 of 2) Figure 10-20 Entering the stop() method
  • 442.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 442 10.5 Moving with Tween Animation • Tween effects are transitions that change objects from one state to another − Alpha: transitions an object from one level of transparency to another, where 0.0 is transparent and 1.0 is opaque − Rotate: spins an object from one angular position to another − Scale: transitions the size of an object (grow or shrink) on an X/Y scale − Translate: moves the object vertically or horizontally by a percentage relative to the element width
  • 443.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 443 10.6 Launching Tween Animation (1 of 3) • The "Start Tween Animation" button in the Northern Lights Animation app triggers two actions within the second onClick() method − The stop() method concludes the Frame animation − A startActivity intent launches a second Activity named Tween.java • A new Java class and a second XML layout file must be created for the new Activity
  • 444.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 444 10.6 Launching Tween Animation (2 of 3) Figure 10-22 Creating the Tween.java class and activity_tween layout
  • 445.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 445 10.6 Launching Tween Animation (3 of 3) Figure 10-23 The startActivity launches the Tween class
  • 446.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 446 10.7 Adding the Layout for the Tween Image (1 of 2) • The second Activity, Tween.java, defines a second layout named activity_tween.xml • This XML layout contains a single ImageView control called ivTween − It references the fifth image named lights5 that will be rotated with the Tween animation
  • 447.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 447 10.7 Adding the Layout for the Tween Image (2 of 2) Figure 10-24 ImageView control coded in activity_tween.xml
  • 448.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 448 10.8 Setting Tween Animation Attributes (1 of 4) • Android uses an XML file-creator utility that supports 10 different resource types that can be set as attributes − The default type is Layout − The Tween animation resource type can be used to create an XML file for a Tween animation − You use this type with rotate as the root element to code a rotation • Attributes set within the rotate element's opening tag are used to define the Tween animation
  • 449.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 449 10.8 Setting Tween Animation Attributes (2 of 4) • Rotation attributes: − android:fromDegrees="0" and android:toDegrees="359" spin the object from 0 to 359 degrees (360 degrees total) − android:pivotX="50%" and android:pivotY="50%" set the pivot point for the rotation at the center of the object − android:duration="2000" sets the duration for each rotation to 2,000 milliseconds − android:repeatCount="5" will rotate the object six times (it sets the number of rotations after the initial rotation)
  • 450.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 450 10.8 Setting Tween Animation Attributes (3 of 4) Figure 10-25 New Resource File dialog box
  • 451.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 451 10.8 Setting Tween Animation Attributes (4 of 4) Figure 10-26 The rotate attributes in rotation.xml within the anim folder
  • 452.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 452 10.9 Coding the startAnimation() Method (1 of 2) • The StartAnimation() method begins animating a View object by calling the AnimationUtils class utilities to access the resources necessary to load the animation • A Tween animation can perform a series of simple transformations with startAnimation(), changing position, size, rotation, and/or transparency on the contents of a View object
  • 453.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 453 10.9 Coding the startAnimation() Method (2 of 2) Figure 10-28 Image rotates using Tween animation (completed code for Tween.java)
  • 454.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 454 Activity 10.2: Knowledge Check 1. Which tween effect makes an object appear to grow or shrink? 2. What files were added to the Northern Lights Animation app to create the image rotation effect? 3. Which rotate attribute or attributes are used to set the point around which the object rotates? 4. How can you apply a Tween animation to a View object in order to launch the tween effect?
  • 455.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 455 Activity 10.2: Knowledge Check Answers (1 of 2) 1. Which tween effect makes an object appear to grow or shrink? Answer: scale 2. What files were added to the Northern Lights Animation app to create the image rotation effect? Answer: Tween.java and activity_tween.xml 3. Which rotate attribute or attributes are used to set the point around which the object rotates? Answer: pivotX and pivotY
  • 456.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 456 Activity 10.2: Knowledge Check Answers (2 of 2) 4. How can you apply a Tween animation to a View object in order to launch the tween effect? Answer: Call the startAnimation() method on the View object
  • 457.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 457 10.10 Changing the Emulator to Landscape Orientation • The default screen orientation layout for the emulator is vertical • Shortcuts for switching to a landscape orientation: − On a PC: Fn + left Ctrl + F12 or 7 on the keypad with Num Lock off − On a Mac: Fn + Ctrl + F12 • Running and Testing the Application − Click the Run app button − Test the two buttons' functionality − Practice changing the orientation of the emulator
  • 458.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 458 Self-Assessment 1. What is your favorite app that features animations prominently? How are they utilized in this app, and how do they contribute to your experience as a user? 2. Think about the categories of apps you would like to develop as a professional. To what extent, and in what capacity, would they incorporate animations? Which of the two types of Android animations you learned about in Chapter 10 do you expect these effects would be coded as?
  • 459.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 459 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 460.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 460 460 Android Bootcamp for Developers Using Java, 4e Chapter 11: Discover! Persistent Data Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 461.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 461 Chapter Objectives When you complete this chapter, you will be able to: • 11.01 Create an Android project using persistent data • 11.02 Use different types of shared preferences • 11.03 Use internal and external storage • 11.04 Save to a database connection • 11.05 Save data using a network connection • 11.06 Write persistent data using a SharedPreferences object • 11.07 Retrieve data using the getString() method Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 462.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 462 11.1 Creating an Android Project Using Persistent Data (1 of 4) Figure 11-1 Electric Car Financing app Figure 11-2 Car payment computed using persistent data in second Activity
  • 463.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 463 11.1 Creating an Android Project Using Persistent Data (2 of 4) • Steps required to create the Electric Car Financing app: 1. Add strings to the String table. 2. Add images to the drawable folder. 3. Design two XML layouts for the first and second Activity. 4. Instantiate the XML components in the first Activity. 5. Establish a SharedPreferences object to store the data entered. 6. Write data to the SharedPreferences object.
  • 464.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 464 11.1 Creating an Android Project Using Persistent Data (3 of 4) • Steps required to create the Electric Car Financing app (continued): 7. Launch a second Activity. 8. Initialize the XML components on the second Activity. 9. Retrieve the data from the SharedPreferences object. 10.Calculate the monthly payment for the car loan and display the appropriate image for the number of loan years using an If structure. 11.Display the monthly payment on the second Activity.
  • 465.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 465 11.1 Creating an Android Project Using Persistent Data (4 of 4) • Persistent data stores values permanently by placing the information in a file • Persistent data can be stored in five ways in Android applications: − Shared preferences, which stores private data in key-value pairs − Internal storage, which stores private data in the memory of the device − External storage, which stores data that can be available to other apps on shared external storage − SQLite database, which stores structured data in a private database − Network connection, which stores data on a web server
  • 466.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 466 11.2 Using Different Types of Shared Preferences • The SharedPreferences object can be used to save any primitive data: Booleans, floats, ints, longs, and strings • Shared preferences are best when your app needs to save small chunks of data in a name/value pair • Steps necessary to save persistent data in a SharedPreferences file: − Obtain an instance of the SharedPreferences file − Create a SharedPreferences.Editor object − Assign values to SharedPreferences objects using the putString() method − Save the values to the preferences file using the commit() method
  • 467.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 467 11.3 Using Internal and External Storage (1 of 2) • Internal storage means storing the information directly on the device’s internal drive − Saved files on the device are available only to the app that created the files − Be careful: insufficient internal storage space can drastically affect the speed of an Android device and its battery life
  • 468.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 468 11.3 Using Internal and External Storage (2 of 2) • Android apps can save persistent data to external storage, e.g., the device’s SD (Secure Digital) card • All applications can read and write files placed on the external storage and the Android smartphone or tablet owner can remove them • To use external storage, the following permissions are necessary in the Android Manifest file: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • 469.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 469 11.4 Saving to a Database Connection • A database is the perfect choice for storing a large amount of data • SQLite is a lightweight, preloaded mobile database engine • SQLite has been available since the Cupcake 1.5 version of Android • It occupies a small amount of disk memory • Android apps model data items in tables and columns, with optional relationships between the entities within the database • The tables can be queried using SQL statements (SQL stands for Structured Query Language)
  • 470.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 470 Activity 11.1: Knowledge Check 1. What are the five ways to store persistent data in an Android application? 2. What step must you take before you can use external storage to save Android app data?
  • 471.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 471 Activity 11.1: Knowledge Check Answers 1. What are the five ways to store persistent data in an Android application? Answer: SharedPreferences object (stores private data in key-value pairs) Internal storage (stores private data in the memory of the device) External storage (stores data that can be available to other apps on shared external storage) SQLite database (stores structured data in a private database) Network connection (stores data on a web server) 2. What step must you take before you can use external storage to save Android app data? Answer: Add the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions to the Android Manifest file.
  • 472.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 472 11.5 Saving Data Using a Network Connection (1 of 20) • If connected to the Internet (a 4G/5G or Wi-Fi connection), persistent data can be stored and retrieved using a web service • Before an app attempts to connect to a network, it should check to see whether an Internet connection is available • If a connection is not available, the user cannot save or retrieve the persistent data
  • 473.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 473 11.5 Saving Data Using a Network Connection (2 of 20) • Creating XML Layout Files − First, create the Electric Car Financing project with API 23: Android 6.0 (Marshmallow) as the minimum SDK − Load the images into the drawable folder − Enter the strings for display into the String table − Drag into and position components within the emulator: a TextView, three EditText Number components, a Button, and an ImageView − Modify the XML code to customize the components
  • 474.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 474 11.5 Saving Data Using a Network Connection (3 of 20) Figure 11-3 Setting up the Electric Car Financing project
  • 475.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 475 11.5 Saving Data Using a Network Connection (4 of 20) Figure 11-4 Image files in the drawable folder
  • 476.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 476 11.5 Saving Data Using a Network Connection (5 of 20) Figure 11-5 String values for the app
  • 477.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 477 11.5 Saving Data Using a Network Connection (6 of 20) <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="@string/tvTitle" android:textSize="40sp" app:layout_constraintBottom_toBottomOf="@+id/ivOpening" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.067" />
  • 478.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 478 11.5 Saving Data Using a Network Connection (7 of 20) <EditText android:id="@+id/tvYears" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvTitle" android:layout_centerHorizontal="true" android:ems="10" android:hint="@string/hint1" android:inputType="number" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.423" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.208" />
  • 479.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 479 11.5 Saving Data Using a Network Connection (8 of 20) <EditText android:id="@+id/tvLoan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvYears" android:layout_centerHorizontal="true" android:ems="10" android:hint="@string/hint2" android:inputType="number" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="@+id/ivOpening" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.423" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.334" />
  • 480.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 480 11.5 Saving Data Using a Network Connection (9 of 20) <EditText android:id="@+id/tvInterest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvLoan" android:layout_centerHorizontal="true" android:ems="10" android:hint="@string/hint3" android:inputType="numberDecimal" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.423" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.463" />
  • 481.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 481 11.5 Saving Data Using a Network Connection (10 of 20) <Button android:id="@+id/btPayment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvInterest" android:layout_centerHorizontal="true" android:layout_marginBottom="276dp" android:backgroundTint="#000000" android:text="@string/btPayment" android:textSize="35sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.458" app:layout_constraintStart_toStartOf="parent" />
  • 482.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 482 11.5 Saving Data Using a Network Connection (11 of 20) <ImageView android:id="@+id/ivOpening" android:layout_width="346dp" android:layout_height="242dp" android:layout_below="@+id/btPayment" android:layout_centerHorizontal="true" android:contentDescription="@string/ivDescription" android:src="@drawable/opening" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.441" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
  • 483.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 483 11.5 Saving Data Using a Network Connection (12 of 20) • Creating a Second Activity and XML Layout − Create a second Activity, which consists of a second class and a second XML layout file − Drag into and position within the emulator the UI components: two TextView components and one ImageView component − Modify the layout XML code to customize the components
  • 484.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 484 11.5 Saving Data Using a Network Connection (13 of 20) Figure 11-7 Creating a second class named Payment and a second XML layout named activity_payment
  • 485.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 485 11.5 Saving Data Using a Network Connection (14 of 20) <TextView android:id="@+id/tvTitle2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="@string/tvTitle" android:textSize="35sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.101" />
  • 486.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 486 11.5 Saving Data Using a Network Connection (15 of 20) <TextView android:id="@+id/tvMonthlyPayment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvTitle2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.27" />
  • 487.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 487 11.5 Saving Data Using a Network Connection (16 of 20) <ImageView android:id="@+id/ivYears" android:layout_width="396dp" android:layout_height="285dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:contentDescription="@string/ivDescription" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.935" /> </androidx.constraintlayout.widget.ConstraintLayout>
  • 488.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 488 11.5 Saving Data Using a Network Connection (17 of 20) • Instantiating the XML Components − Instantiate the EditText components from the first Activity − Instantiate the Button component from the first Activity, creating an OnClickListener stub − Initialize the variables that will be used for calculations to values parsed from the user's entries in the EditText components
  • 489.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 489 11.5 Saving Data Using a Network Connection (18 of 20) Figure 11-9 EditText components instantiated
  • 490.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 490 11.5 Saving Data Using a Network Connection (19 of 20) Figure 11-10 Button OnClickListener stub
  • 491.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 491 11.5 Saving Data Using a Network Connection (20 of 20) Figure 11-11 Variables intYears, intLoan, and decInterest assigned
  • 492.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 492 11.6 Writing Persistent Data with SharedPreferences (1 of 5) • Data is saved to an XML file as a key-value pair − The key is a string such as “key1” that uniquely identifies the preference − The value is the data represented as a string, int, long, float, or Boolean • Data types supported by the SharedPreferences class: − putString()—stores string values − putInt()—stores integer values − putLong()—stores long values − putFloat()—stores float values − putBoolean()—stores Boolean values
  • 493.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 493 11.6 Writing Persistent Data with SharedPreferences (2 of 5) • Java syntax for storing data with the SharedPreferences object: final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("key1", intYears); editor.putInt("key2", intLoan); editor.putFloat("key3", decInterest); editor.commit(); • Within the editor, you can also remove a single preference pair using the remove() method or remove all preferences using the clear() method
  • 494.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 494 11.6 Writing Persistent Data with SharedPreferences (3 of 5) Figure 11-13 Three values (persistent data) written in an XML data file
  • 495.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 495 11.6 Writing Persistent Data with SharedPreferences (4 of 5) • Launching the Second Activity − Add the statement startActivity(new Intent(MainActivity.this, Payment.class)); to the onClick() method • Instantiating the Second Activity Components − Instantiate the TextView and ImageView components
  • 496.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 496 11.6 Writing Persistent Data with SharedPreferences (5 of 5) Figure 11-15 Instantiating the TextView and ImageView components in Payment.java
  • 497.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 497 11.7 Retrieving Data Using getString() Methods (1 of 7) • You do not need an editor to read saved data in SharedPreferences • Retrieve the SharedPreferences object and use the appropriate method to retrieve a key’s value by name − getString()—retrieves string values − getInt()—retrieves integer values − getLong()—retrieves long values − getFloat()—retrieves float values − getBoolean()—retrieves Boolean values
  • 498.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 498 11.7 Retrieving Data Using getString() Methods (2 of 7) • Each "get" method has two parameters: the preference key string and a default value to return if the preference is undefined • Java syntax for creating a SharedPreferences instance and loading data from the persistent data XML file: SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); int intYears = sharedPref.getInt("key1", 0); int intLoan = sharedPref.getInt("key2", 0); float decInterest = sharedPref.getFloat("key3", 0);
  • 499.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 499 11.7 Retrieving Data Using getString() Methods (3 of 7) Figure 11-17 SharedPreferences values are retrieved
  • 500.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 500 11.7 Retrieving Data Using getString() Methods (4 of 7) • Displaying the Correct ImageView Component − An ImageView control can display an image by assigning a source path (android:src="drawable/filename") in the XML layout file − Alternatively, it can display an image by dynamically assigning the image within the Java code: image.setImageResource(R.drawable.three); − For the Electric Car Financing app, a monthly payment is calculated and formatted for display − A nested If decision structure is used to select an image to display
  • 501.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 501 11.7 Retrieving Data Using getString() Methods (5 of 7) Figure 11-19 The monthly payment is displayed in the second Activity
  • 502.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 502 11.7 Retrieving Data Using getString() Methods (6 of 7) Figure 11-21 Closing Else statement
  • 503.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 503 11.7 Retrieving Data Using getString() Methods (7 of 7) • Running and Testing the Application − Run the app in the emulator − The first Activity saves data you enter to an XML file using the SharedPreferences object − The second Activity launches the Payment.java file, which retrieves the stored data and calculates the monthly payment for the car loan − The monthly payment is shown, and a decision structure determines which image should be displayed to indicate the loan term
  • 504.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 504 Activity 11.2: Knowledge Check 1. What is the difference between a key and a value in a SharedPreferences XML file? 2. What two SharedPreferences class methods would you call to store an integer value? 3. What methods of the SharedPreferences object are used to retrieve Android data? 4. What Java method can you use to dynamically assign an image to display within an ImageView component? 5. Answer: setImageResource()
  • 505.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 505 Activity 11.2: Knowledge Check Answers (1 of 2) 1. What is the difference between a key and a value in a SharedPreferences XML file? Answer: The key is a string such as “key1” that uniquely identifies the preference, and the value is the data represented as a string, int, long, float, or Boolean value. 2. What two SharedPreferences class methods would you call to store an integer value? Answer: putInt() and commit()
  • 506.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 506 Activity 11.2: Knowledge Check Answers (2 of 2) 3. What methods of the SharedPreferences object are used to retrieve Android data? Answer: getString() retrieves string values getInt() retrieves integer values getLong() retrieves long values getFloat() retrieves float values getBoolean() retrieves Boolean values 4. What Java method can you use to dynamically assign an image to display within an ImageView component? Answer: setImageResource()
  • 507.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 507 Self-Assessment 1. Think about the kinds of Android applications you anticipate working on as a developer. What kinds of data, and what quantities of data, would these apps need to store and retrieve to function usefully? 2. Which of the methods for storing persistent data that you learned about in Chapter 11 do you think should be used for the apps you envisioned? 3. Consider the personal data that many apps store about individuals. How much do you know about the coding practices and other procedures used to safeguard personal data? What else do you need to learn to write apps that offer reasonable data protection?
  • 508.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 508 Summary • Click the link to review the objectives for this presentation. Chapter objectives
  • 509.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 509 509 Android Bootcamp for Developers Using Java, 4e Chapter 12: Finale! Publishing Your Android App Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 510.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 510 Chapter Objectives When you complete this chapter, you will be able to: • 12.01 Describe Google Play • 12.02 Target various device configurations and languages • 12.03 Test your app before publishing • 12.04 Create an Android App Bundle • 12.05 Prepare promotional materials to upload to Google Play • 12.06 Publish your app on Google Play Hoisington, Android Bootcamp for Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
  • 511.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 511 12.1 Using Google Play (1 of 3) • There are many distribution networks • Apps can be published to: − Google Play (Google’s app store, the largest marketplace) − Amazon Appstore − Huawei AppGallery − Xiaomi Mi GetApps − Aurora
  • 512.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 512 12.1 Using Google Play (2 of 3) • Steps to publish an app: 1. Test the app 2. Prepare the app for publication 3. Create an AAB package and digitally sign the application 4. Prepare promotional materials 5. Publish the app to Google Play
  • 513.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 513 12.1 Using Google Play (3 of 3) • Google Play is a digital repository that serves as the online storefront for paid and free Android apps − URL: https://play.google.com − Contains the features and services of Android apps, Google Music, and Google e-books − Provides free cloud storage services − Used by over 190 countries − Apps selected on Google Play install directly to the Android device − Part of the default setup on new Android devices
  • 514.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 514 12.2 Targeting Device Configurations and Languages (1 of 4) • You can reach a larger audience by translating your app into multiple languages • To support multiple languages: − Create a resource directory for each language in the strings directory (the strings.xml file) using the Translations Editor − One application then recognizes multiple languages • You can use the Google Translate service (https://translate.google.com) to translate into over 100 different languages
  • 515.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 515 12.2 Targeting Device Configurations and Languages (2 of 4) • Adding Localization Using the Translations Editor − The Android Studio Translations Editor supports multiple languages − Localization: the process of adapting an app or web page to a language while considering cultural differences  Differences include whether the language is read left-to-right or right-to- left, calendar layout, measurements, currency, date and time formats, and traditions of the target audience − The globe-shaped Add Locale icon in the Translations Editor provides access to a list of locales around the world
  • 516.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 516 12.2 Targeting Device Configurations and Languages (3 of 4) Figure 12-2 Locale listing in the Translations Editor
  • 517.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 517 12.2 Targeting Device Configurations and Languages (4 of 4) Figure 12-4 Red text on key designates missing translation
  • 518.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 518 12.3 Testing Your App before Publication • Use the Android Studio built-in emulators to test an app's design and functionality • Check real-world functionality by using the Android Debug Bridge (ADB), which allows Android Studio to communicate with a connected Android device − Access the Settings > Developer options on the device and check the USB debugging option − Install a USB driver for ADB on the computer (Windows only) − Run the app in Studio and select the connected device
  • 519.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 519 12.4 Creating an Android App Bundle (1 of 5) • The required app package format for Google Play is Android App Bundle (AAB) format • An .aab file is similar to a .zip file that contains the application, the manifest file, and all associated resources, such as image files, music, and other required content • You use the Android Studio Build menu to build a release-ready .aab file signed with your private key and optimized for publication − All Android apps must be digitally signed with a certificate that identifies the author and establishes trust relationships between applications
  • 520.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 520 12.4 Creating an Android App Bundle (2 of 5) Figure 12-6 Using the Build menu in Android Studio
  • 521.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 521 12.4 Creating an Android App Bundle (3 of 5) Figure 12-7 Generate Signed Bundle or APK dialog box
  • 522.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 522 12.4 Creating an Android App Bundle (4 of 5) Figure 12-8 Creating a key in the Generate Signed Bundle or APK dialog box
  • 523.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 523 12.4 Creating an Android App Bundle (5 of 5) Figure 12-9 Selecting the debug option
  • 524.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 524 Activity 12.1: Knowledge Check 1. Which tool is used to communicate with a connected Android device through Android Studio? 2. When you adapt an app or web page to a language while considering cultural differences such as the direction in which the language is read, date and time formats, units of measurement, and currency, you are adding _____. 3. To publish an app on Google Play, you must package it in _____ format, which has the _____ file extension. 4. What is used to sign your Android application with information from your local system?
  • 525.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 525 Activity 12.1: Knowledge Check Answers 1. Which tool is used to communicate with a connected Android device through Android Studio? Answer: Android Debug Bridge (ADB) 2. When you adapt an app or web page to a language while considering cultural differences such as the direction in which the language is read, date and time formats, units of measurement, and currency, you are adding _____. Answer: localization 3. To publish an app on Google Play, you must package it in _____ format, which has the _____ file extension. Answer: Android App Bundle (AAB), .aab 4. What is used to sign your Android application with information from your local system? Answer: A private key or certificate
  • 526.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 526 12.5 Preparing Promotional Materials to Upload (1 of 11) Figure 12-10 National Geographic Android app from Google Play
  • 527.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 527 12.5 Preparing Promotional Materials to Upload (2 of 11) • Providing Images − Application icon (required): 512 x 512 pixels in size, stored in a 32-bit .png file − Screenshots (2 required; up to 8): 480 x 320, 800 x 480, or 854 x 480 pixels in size; PNG files − Video that demonstrates the app (optional): should highlight the top features and be between 30 seconds and 2 minutes  Can link to the video on YouTube
  • 528.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 528 12.5 Preparing Promotional Materials to Upload (3 of 11) • Providing a Description − An app description provides a quick overview to the purpose of the app and what it does − Include features the app provides − You want to sell the app to the widest audience possible − Motivate users to download the app − Revise the description as you update the app − Utilize quotes from positive reviews when possible
  • 529.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 529 12.5 Preparing Promotional Materials to Upload (4 of 11) Figure 12-12 National Geographic reviews
  • 530.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 530 12.5 Preparing Promotional Materials to Upload (5 of 11) • Providing a Description (continued) − Pricing an app  Users may try a free app, then buy additional features, including in-app purchases  Some users will never pay for an average app, but many will pay for a great one − When you upload an app to Google Play, you select one or more of the application categories to classify it
  • 531.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 531 12.5 Preparing Promotional Materials to Upload (6 of 11) Category Example Types of Apps Books & Reference Book readers, reference books, textbooks, dictionaries, thesauruses, wikis Business Document editors/readers, package tracking, remote desktops, email management, job searches Comics Comic players, comic titles Communications Messaging, chat/IM, dialers, address books, browsers, call management Education Exam preparation, study aids, vocabulary, educational games, language learning Finance Banking, payment, ATM finders, financial news, insurance, taxes, portfolio/trading, tip calculators Health & Fitness Personal fitness, workout tracking, diet and nutritional tips, health and safety
  • 532.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 532 12.5 Preparing Promotional Materials to Upload (7 of 11) Category Example Types of Apps Libraries & Demo Software libraries Lifestyle Recipes, style guides Media & Video Subscription movie services, remote controls, media/video players Medical Drug and clinical references, calculators, handbooks for healthcare providers, medical journals and news Music & Audio Music services, radios, music players News & Magazines Newspapers, news aggregators, magazines, blogging Personalization Wallpapers, live wallpapers, home screens, lock screens, ring tones Photography Cameras, photo-editing tools, photo management and sharing
  • 533.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 533 12.5 Preparing Promotional Materials to Upload (8 of 11) Category Example Types of Apps Productivity Notepads, to-do lists, keyboards, printing, calendars, backup, calculators, conversion Shopping Online shopping, auctions, coupons, price comparison, grocery lists, product reviews Social Social networking, check-in, blogging Sports Sports news and commentary, score tracking, fantasy team management, game coverage Tools System utility tools Travel & Local City guides, local business information, trip management tools Weather Weather reports
  • 534.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 534 12.5 Preparing Promotional Materials to Upload (9 of 11) • Including Contact and Social Networks − A social networking presence for your app enables you to build relationships with customers and communicate with your fans • Setting Versions for your App − After an app is published, you can update it with new features and new SDK platforms − Allows the Android OS to check whether a newer version of the app is available from the Google Play Store and then prompt the user to upgrade the app, if necessary
  • 535.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 535 12.5 Preparing Promotional Materials to Upload (10 of 11) • Setting Versions for your App (continued) − Syntax to set the version of code and version name number within the AndroidManifest.xml file: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/androi d" xmlns:tools="http://schemas.android.com/tools" android:versionCode="1" android:versionName="1.0">
  • 536.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 536 12.5 Preparing Promotional Materials to Upload (11 of 11) • Registering for a Google Play Account − A Google account is needed − Register at https://play.google.com/apps/publish − A one-time payment of $25 is required to register as an Android application developer and enrolls you in a Google Merchant account − If you charge for apps, you also obtain a Google Wallet account and Google Merchant disperses revenue for application sales  For the first $1 million of revenue, you get 85% and the phone carriers get 15%  For profits >$1 million, the service fee increases to 30%
  • 537.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 537 12.6 Publishing Your App to Google Play (1 of 4) • Once you create an account, the Developer Console pages take you through the steps to upload your unlocked app .aab file and the promotional assets − Maximum size for the .aab file: 150 MB • Ensure your app complies with Google's content policies and with local laws • After launching your app, you can visit your Dashboard page to get an overview of the app's key metrics, trends, and insights
  • 538.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 538 12.6 Publishing Your App to Google Play (2 of 4) Figure 12-15 All apps page on Google Play Console
  • 539.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 539 12.6 Publishing Your App to Google Play (3 of 4) Figure 12-16 Create app website
  • 540.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 540 12.6 Publishing Your App to Google Play (4 of 4) Figure 12-17 Google Play developers Dashboard
  • 541.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 541 Activity 12.2: Knowledge Check 1. How much does it cost to register as a developer with Google Play? 2. What are the pixel dimensions of an application icon? 3. In what format should application icons be stored? 4. What is the maximum supported file size for the .aab file at Google Play?
  • 542.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 542 Activity 12.2: Knowledge Check Answers 1. How much does it cost to register as a developer with Google Play? Answer: $25 2. What are the pixel dimensions of an application icon? Answer: 512 x 512 3. In what format should application icons be stored? Answer: PNG (32-bit .png file) 4. What is the maximum supported file size for the .aab file at Google Play? Answer: 150 MB
  • 543.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 543 Self-Assessment 1. Now that you have completed the lessons in this textbook, how well prepared do you feel to write and publish your own Android apps? 2. How prepared do you feel to join a team of professional developers working on an Android app? 3. What aspects of Android app development would you like to study further or gain greater experience with to improve your relevant skills?
  • 544.
    Hoisington, Android Bootcampfor Developers Using Java, 4th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 544 Summary • Click the link to review the objectives for this presentation. Chapter objectives

Editor's Notes

  • #3 Instructions: Task: This is an introductory exercise for the beginning of a course. Objectives: To provide an informal way for participants to learn about each other and begin working together. This exercise also gives students practice speaking in front of a group, introduces them to interviewing, and gives the instructor insight into who the students are and what is important to them.  Steps: Break the class into pairs of students. Have them interview each other to discover interesting facts and then introduce each other to the class.  Average Time in Class: 30–45 min Note: To adapt this activity for an online learning environment, conduct the interviews using a web conferencing tool (e.g., Zoom or Microsoft Teams). When you break the class into pairs, send the pairs into breakout rooms for 10 minutes to interview each other. When the class comes back together, give each pair 2–4 minutes to introduce each other to the rest of the class.
  • #4 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #5 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #15 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #16 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #17 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #18 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #44 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #45 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #47 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #50 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #51 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #80 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #81 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #82 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #83 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #98 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #99 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #100 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #101 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #104 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #107 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #108 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #109 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #125 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #126 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #140 Table 3-2 Primitive data types in Java
  • #145 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #146 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #151 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #154 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #155 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #156 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #163 Table 4-1 Launcher icon sizes
  • #176 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #177 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #178 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #179 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #192 Table 4-3 Relational operators
  • #197 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #198 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #204 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #207 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #208 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #224 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #225 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #237 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #238 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #240 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #243 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #244 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #262 Table 6-2 Methods used in the life cycle of an Activity
  • #268 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #269 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #285 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #286 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #287 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #290 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #291 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #296 Table 7-1 GridView component attributes
  • #303 Table 7-2 Animals array
  • #309 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #310 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #313 Table 7-3 Arguments in the onItemClick() method
  • #324 Table 7-4 Popular ScaleType options
  • #329 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #330 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #331 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #332 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #335 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #336 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #347 Table 8-1 Table for Linear layout
  • #354 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #355 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #364 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #365 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #372 Table 8-2 XML code for common themes
  • #374 Instructions: Use the Self-Assessment question to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #377 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #398 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #399 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #400 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #405 Code Syntax box from page 316
  • #408 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #409 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #410 Instructions: Use the Self-Assessment question to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #413 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #414 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #439 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #440 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #455 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #456 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #457 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #459 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #462 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #471 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #472 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #505 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #506 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #507 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #508 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.
  • #511 Instructions: Briefly review with students the major concepts you will be covering during this class. There is one objective for every major A-Head section of the chapter.
  • #525 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #526 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #532 Table 12-1 App category types on Google Play
  • #533 Table 12-1 App category types on Google Play (continued)
  • #534 Table 12-1 App category types on Google Play (continued)
  • #542 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #543 Instructions: Use the Knowledge Check questions to periodically pose a question to students during class to gauge how well they can recall the material that was presented.
  • #544 Instructions: Use the Self-Assessment questions to encourage students to evaluate their progress or goals in the course, as well as determine how they might apply their learning or grow as an individual.