Android Boot Camp for Developers Using Java Comprehensive

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 3: Engage! Android User Input, Variables, and Operations Android Boot Camp for Developers Using Java, 2 nd Ed. 1

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 • Display a hint using the Hint property • Develop the user interface using a Spinner control • Add text to the String table Android Boot Camp for Developers Using Java, 2 nd Ed. 2

Objectives • • (continued) Add a prompt to the Spinner control Declare variables to hold data Code the Get. Text() method Understand arithmetic operations Convert numeric data Format numeric data Code the Set. Text() method Run the completed app in the emulator Android Boot Camp for Developers Using Java, 2 nd Ed. 3

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 Android Boot Camp for Developers Using Java, 2 nd Ed. 4

Android Themes (continued) • Previewing a Theme – Check the main. xml file in the emulator to see what your screen looks like Android Boot Camp for Developers Using Java, 2 nd Ed. 5

Android Themes (continued) • Updating the Theme in the Android Manifest File – Insert this code into the Android. Manifest. xml file android: theme="@android: style/Theme. Black. No. Title. Bar“ Android Boot Camp for Developers Using Java, 2 nd Ed. 6

Simplifying User Input – The onscreen keyboard is called a soft keyboard • 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 Android Boot Camp for Developers Using Java, 2 nd Ed. 7

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 Android Boot Camp for Developers Using Java, 2 nd Ed. 8

Simplifying User Input Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 9

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 txt. Tickets instead of txt. Text 1 • Using the String Table – Uses the file strings. xml – A string is a series of alphanumeric characters that can include spaces. – Localization is the use of the String table to change text based on the user’s preferred language. Android Boot Camp for Developers Using Java, 2 nd Ed. 10

Simplifying User Input • Using the String Table Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) (Continued) 11

Simplifying User Input • Using the String Table (continued) (Continued) Android Boot Camp for Developers Using Java, 2 nd Ed. 12

Simplifying User Input (continued) • String Arrays – Multiple text strings can be stored in the strings. xml file – A control such as a Spinner can be populated with a string array Android Boot Camp for Developers Using Java, 2 nd Ed. 13

Simplifying User Input Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 14

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) Android Boot Camp for Developers Using Java, 2 nd Ed. 15

Simplifying User Input (continued) • Using the 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 prompt, which can be used to display instructions at the top of the Spinner control, also is stored in strings. xml and is named prompt – The Spinner property called Entries connects the String Array to the Spinner control for display in the application Android Boot Camp for Developers Using Java, 2 nd Ed. 16

Simplifying User Input (continued) • Adding the Button, Text. View, and Image. View Controls Android Boot Camp for Developers Using Java, 2 nd Ed. 17

Simplifying User Input (continued) • Coding the Edit. Text 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 Edit. Text tickets=(Edit. Text) find. View. By. Id(R. id. txt. Tickets); Android Boot Camp for Developers Using Java, 2 nd Ed. 18

Simplifying User Input Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 19

Simplifying User Input (continued) • Coding the Spinner Control – final Spinner group = (Spinner) find. View. By. Id(R. id. txt. Group); • Coding the Button. Control – final Text. View result = ((Text. View)find. View. By. Id(R. id. txt. Result) ); Android Boot Camp for Developers Using Java, 2 nd Ed. 20

Simplifying User Input Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 21

Declaring Variables (continued) • Declaring Variables – Declare the variable – Assign a value to the variable • Primitive Data Types Android Boot Camp for Developers Using Java, 2 nd Ed. 22

Declaring Variables (continued) • String Data Type – The String type is a class and not a primitive 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 Android Boot Camp for Developers Using Java, 2 nd Ed. 23

Declaring Variables Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 24

Declaring Variables (continued) • Get. Text() Method – Read data stored in the Edit. Text control with the Get. Text() method – Data is read in as a string, by default – A Parse class is used to convert strings into numbers Android Boot Camp for Developers Using Java, 2 nd Ed. 25

Working with Mathematical Operations Android Boot Camp for Developers Using Java, 2 nd Ed. 26

Working with Mathematical Operations • Arithmetic Operators Android Boot Camp for Developers Using Java, 2 nd Ed. 27

Working with Mathematical Operations (cont’d) • Formatting Numbers – Currency format requires a dollar sign, a comma (if needed), a decimal point, and two decimal places – Java has a class called Decimal. Format that provides patterns, such as $###, ###. ## for displaying on the Android screen Android Boot Camp for Developers Using Java, 2 nd Ed. 28

Displaying Android Output • Get. Selected. Item() Method – The Get. Selected. Item() method returns the text label of the currently selected Spinner item • Set. Text () Method – The Set. Text() method displays text in a Text. View control result. set. Text("Total Cost for " + group. Choice + " is " + currency. format(total. Cost)); Android Boot Camp for Developers Using Java, 2 nd Ed. 29

Displaying Android Output Android Boot Camp for Developers Using Java, 2 nd Ed. (continued) 30

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 Edit. Text class to extract text users have entered and store that text in a variable • Use Get. Text() to get data and Set. Text() to display data Android Boot Camp for Developers Using Java, 2 nd Ed. 31

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 Get. Selected. Item() 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 Android Boot Camp for Developers Using Java, 2 nd Ed. 32
- Slides: 32