chapter 3: engage! android user input, variables, and operations
DESCRIPTION
Chapter 3: Engage! Android User Input, Variables, and Operations. Objectives. In this chapter, you learn to: Use an Android theme Add a theme to the Android Manifest file Develop the user interface using Text Fields State the role of different Text Fields - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/1.jpg)
Chapter 3: Engage! Android User Input, Variables, and Operations
![Page 2: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/2.jpg)
ObjectivesIn this chapter, you learn to:• Use an Android theme• Add a theme to the Android Manifest file• Develop the user interface using Text Fields• State the role of different Text Fields• Display a hint using the Hint property• Develop the user interface using a Spinner control• Add text to the String table
2Android Boot Camp for Developers using Java
![Page 3: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/3.jpg)
Objectives (continued)
• Add a prompt to the Spinner control• Declare variables to hold data• Code the GetText() method• Understand arithmetic operations• Convert numeric data• Format numeric data• Code the SetText() method• Run the completed app in the emulator
3Android Boot Camp for Developers using Java
![Page 4: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/4.jpg)
Android Themes
• Engaging the user by requesting input customizes the user experience each time the application is executed
• A theme is a style applied to an Activity or an entire application
• The default theme is a title bar (often gray) with a black background
4Android Boot Camp for Developers using Java
![Page 5: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/5.jpg)
Android Themes (continued)
• Previewing a Theme– Check the main.xml file in the emulator to see what
your screen looks like
5Android Boot Camp for Developers using Java
Figure 3-3 Default Theme Figure 3-4 Holographic Theme
![Page 6: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/6.jpg)
Android Themes (continued)
• Coding a Theme in the Android Manifest File– Insert this code into the AndroidManifest.xml file
android:theme="@android:style/Theme.Black.NoTitleBar“
6Android Boot Camp for Developers using Java
Figure 3-10 Adding the theme to the Android Manifest File
![Page 7: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/7.jpg)
Simplifying User Input
– The onscreen keyboard is called a softkeyboard• Input can be in the form of tapping or gestures (using
two fingers to pan, rotate, or zoom)• Primary design challenge is to simplify user
experiences• Use legible fonts, simplify input, and optimize each
device’s capabilities to maximize user experience
7Android Boot Camp for Developers using Java
Figure 3-13 Onscreen keyboard
![Page 8: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/8.jpg)
Simplifying User Input (continued)
• Android Text Fields– Text Fields are the most common type of mobile
input• Can be free-form plain text• Numbers• A person’s name, password, email, phone number• A date• Multiline text
8Android Boot Camp for Developers using Java
![Page 9: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/9.jpg)
Simplifying User Input (continued)
9Android Boot Camp for Developers using Java
Figure 3-12 Types of Text Fields controls
![Page 10: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/10.jpg)
Simplifying User Input (continued)
• Adding a Text Field– Use the Id property
in the Properties pane to enter a name that begins with the prefix txt
– Use descriptive names like txtTickets instead of txtText1
10Android Boot Camp for Developers using Java
Figure 3-14 TextView control added and formatted
![Page 11: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/11.jpg)
Simplifying User Input (continued)
• Setting the Hint Property for the Text Field– A hint is a short description of a field visible as light-
colored text (called a watermark)
11Android Boot Camp for Developers using Java
Figure 3-17 Hint added to text Field control
![Page 12: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/12.jpg)
Simplifying User Input (continued)
• Coding the EditText Class for the Text Field– A variable is used in programming to contain data
that changes during the execution of a program– Final variables can be initialized but cannot be
changed– Insert this code to create a variable:
final EditText tickets=(EditText) findViewById(R.id.txtTickets);
12Android Boot Camp for Developers using Java
![Page 13: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/13.jpg)
Simplifying User Input (continued)
13Android Boot Camp for Developers using Java
Figure 3-18 Coding the EditText class for the Text Field
![Page 14: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/14.jpg)
Simplifying User Input (continued)
• Android Spinner Control– A Spinner control is similar to a drop-down list for
selecting a single item from a fixed list– The spinner control displays a list of strings called
items in a pop-up window– A string is a series of alphanumeric characters that
can include spaces
14Android Boot Camp for Developers using Java
![Page 15: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/15.jpg)
Simplifying User Input (continued)
• Using the String Table– A file named strings.xml is part of all Android apps
and contains a list of commonly used strings– Android loads the resources from the project’s String
table– Android can select text using Localization which
allows for changing text based on the user’s preferred language
– A prompt is used to display instructions at the top of the spinner control
15Android Boot Camp for Developers using Java
![Page 16: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/16.jpg)
Simplifying User Input (continued)
• Adding a Spinner Control with String Array Entries– A Spinner property named Entries connects the
String Array to the Spinner control• Coding the Spinner Control
16Android Boot Camp for Developers using Java
Figure 3-26 Coding the Spinner Control
![Page 17: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/17.jpg)
Simplifying User Input (continued)
• Adding the Button, TextView, and ImageView Controls
17Android Boot Camp for Developers using Java
Figure 3-27 Adding a Button control
![Page 18: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/18.jpg)
Simplifying User Input (continued)
18Android Boot Camp for Developers using Java
Figure 3-28 Coding the button
![Page 19: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/19.jpg)
Simplifying User Input (continued)
19Android Boot Camp for Developers using Java
Figure 3-29 Adding a TextView control to display results
![Page 20: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/20.jpg)
Simplifying User Input (continued)
20Android Boot Camp for Developers using Java
Figure 3-30 Assigning the TextView control to a variable
![Page 21: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/21.jpg)
Simplifying User Input (continued)
21Android Boot Camp for Developers using Java
Figure 3-31 Adding a ImageView control
![Page 22: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/22.jpg)
Declaring Variables• Primitive Data Types
22Android Boot Camp for Developers using Java
Table 3-1 Primitive data types in Java
![Page 23: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/23.jpg)
Declaring Variables (continued)
• String Data Type– A string can be a character, word, or phrase
• Declaring the Variables– Typically declared at the beginning of an Activity– Variables must be declared before you can use them
23Android Boot Camp for Developers using Java
![Page 24: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/24.jpg)
Declaring Variables (continued)
24Android Boot Camp for Developers using Java
Figure 3-32 Declaring variables for the Activity
![Page 25: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/25.jpg)
Declaring Variables (continued)
• GetText() Method– Read data stored in the EditText control with the
GetText() method– Data is read in as a string, by default– A Parse class is used to convert strings into
numbers
25Android Boot Camp for Developers using Java
Table 3-2 Parse type conversions
![Page 26: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/26.jpg)
Working with Mathematical Operations
26Android Boot Camp for Developers using Java
• Arithmetic Operators
Table 3-3 Java arithmetic operators
![Page 27: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/27.jpg)
Working with Mathematical Operations (cont’d)
27Android Boot Camp for Developers using Java
• Formatting Numbers– Currency format requires a dollar sign, a comma (if
needed), a decimal point, and two decimal places– Java has a class called DecimalFormat that
provides patterns, such as $###,###.## for displaying on the Android screen
Figure 3-34 Calculating and formatting the ticket cost
![Page 28: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/28.jpg)
Displaying Android Output
• GetSelectedItem() Method– The GetSelectedItem() method returns the text
label of the currently selected Spinner item• SetText () Method
– The SetText() method displays text in a TextView control
28Android Boot Camp for Developers using Java
![Page 29: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/29.jpg)
Displaying Android Output (continued)
29Android Boot Camp for Developers using Java
Figure 3-35 Completed code
![Page 30: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/30.jpg)
Summary• Assign a theme to an Activity or an entire
application to prevent apps from looking too similar• Define a theme in the Android Manifest file for each
Activity• Use Text Fields to request input from users• Use a control’s Hint property to provide guidelines
to users so they know what to enter• Use the EditText class to extract text users have
entered and store that text in a variable• Use GetText() to get data and SetText() to display
data30Android Boot Camp for Developers using Java
![Page 31: Chapter 3: Engage! Android User Input, Variables, and Operations](https://reader031.vdocuments.us/reader031/viewer/2022013106/5681610c550346895dd05d1b/html5/thumbnails/31.jpg)
Summary (continued)
• Strings.xml is part of every Android application• You can edit a string in the strings.xml file to update
text anywhere in the application• Use GetSelectedItem() to return the text of the
selected Spinner item• To use a variable, you must first declare it• Variables are declared at the beginning of an
Activity• Convert variables to the correct data type using the
Parse class
31Android Boot Camp for Developers using Java