4
play

4 Tip Calculator App O bj e ct i v e s In this chapter youll: - PDF document

4 Tip Calculator App O bj e ct i v e s In this chapter youll: Design a GUI using a TableLayout . Use the ADT Plugins Outline window in Eclipse to add GUI components to a TableLayout . Directly edit the XML of a GUI layout to


  1. 4 Tip Calculator App O bj e ct i v e s In this chapter you’ll: ■ Design a GUI using a TableLayout . ■ Use the ADT Plugin’s Outline window in Eclipse to add GUI components to a TableLayout . ■ Directly edit the XML of a GUI layout to customize properties that are not available through the Visual Layout Editor and Properties window in Eclipse. ■ Use TextView , EditText and SeekBar GUI components. ■ Use Java object-oriented programming capabilities, including classes, anonymous inner classes, objects, interfaces and inheritance to create an Android app. ■ Programmatically interact with GUI components to change the text that they display. ■ Use event handling to respond to user interactions with an EditText and a SeekBar .

  2. Self-Review Exercises 2 Self-Review Exercises 4.1 Fill in the blanks in each of the following statements: a) An —often called a text box or text field in other GUI technologies—is a subclass of TextView that can display text and accept text input from the user. ANS: EditText. b) Use a to arrange GUI components into rows and columns. ANS: TableLayout. c) When working with more complex layouts like TableLayout s, it’s difficult to see the nested structure of the layout and to place components in the correct nested locations using the Visual Layout Editor. The window makes these tasks easier be- cause it shows the nested structure of the GUI. So, in a TableLayout , you can select the appropriate row and add a GUI component to it. ANS: Outline. d) Class of package android.os represents an app’s state information. ANS: Bundle . e) You implement interface of package android.text to respond to events when the user interacts with an EditText component. ANS: TextWatcher. f) A separate is typically associated with each screen of an app. ANS: activity. g) The method is called by the system when an Activity is starting—that is, when its GUI is about to be displayed so that the user can interact with the Activity . ANS: onCreate . h) As you build your app’s GUI and add resources (such as string s in the strings.xml file or GUI components in the main.xml file) to your app, the ADT Plugin tools generate a class named that contains nested static classes representing each type of resource in your project’s res folder. ANS: R. i) Class (nested in class R )—contains constants for any drawable items, such as images, that you put in the various drawable folders in your app’s res folder. ANS: R. drawable . j) Class (nested in class R )—contains constants for each String in the strings.xml file. ANS: R.string k) Once the layout is inflated, you can get references to the individual widgets using Ac- tivity ’s method. This method takes an int constant for a specific view (that is, a GUI component) and returns a reference to it. ANS: findViewById . l) You use a TableLayout to arrange the GUI components into and . ANS: rows, columns. 4.2 State whether each of the following is true or false . If false , explain why. a) Android requires that you use the GUI component’s class name in each component’s Id property in the XML layout and in each component’s variable name in the Java code. ANS: True. b) You can force an EditText to display a cursor so that the user can’t manipulate the text. ANS: False. You can prevent an EditText from displaying a cursor so that the user can’t ma- nipulate the text.

  3. Tip Calculator App 3 Chapter 4 c) Each component’s Relative weight determines how it should be sized relative to other components. ANS: False. Each component’s Layout weight determines how it should be sized relative to other components. d) As with all Java programs, Android apps have a main method . ANS: False. Android apps don’t have a main method . e) An active (or running) activity is visible on the screen and “has the focus”—that is, it’s in the background. This is the activity the user is interacting with. ANS: False. An active (or running) activity is visible on the screen and “has the focus”— that is, it’s in the foreground . This is the activity the user is interacting with. f) A stopped activity is visible on the screen and is likely to be killed by the system when its memory is needed. ANS: False. A stopped activity is not visible on the screen and is likely to be killed by the system when its memory is needed. g) Method onCreate typically initializes the Activity ’s instance variables and GUI com- ponents. This method should be as simple as possible so that the app loads quickly. In fact, if the app takes longer than five seconds to load, the operating system will display an ANR (Application Not Responding) dialog—giving the user the option to forcibly terminate the app. ANS: True. Exercises 4.3 Fill in the blanks in each of the following statements: a) String literals should be placed in the strings.xml file in the app’s fold- er—especially if you intend to localize your app for use with multiple languages. ANS: res/values . b) You can control whether or not the user can give the focus to an EditText by setting its property. ANS: Focusable. c) A component’s specifies its relative importance with respect to other com- ponents. ANS: Layout weight . d) Class of package android.app provides the basic lifecycle methods of an app. ANS: Activity . e) Interface of package android.text allows you to change the content and markup of text in a GUI. ANS: Editable . f) You implement interface of package android.widget to respond to the user moving the SeekBar ’s thumb. ANS: SeekBar.OnSeekBarChangeListener g) Android apps have four types of components—activities, services, content providers and . ANS: broadcast receivers. h) Throughout its life an activity can be in one of several states—active (or running), paused or . The activity transitions between these states in response to var- ious events. ANS: stopped.

  4. Exercises 4 i) The method is called by the system when the configuration of the device changes during the app’s execution—for example, when the user rotates the device or slides out a keyboard on a device with a hard keyboard. ANS: onSaveInstanceState . j) Class (nested in class R )—contains constants for the GUI components in your XML layout files. ANS: R.id . k) Method setContentView uses a received constant to load the corresponding XML doc- ument, which is then parsed and converted into the app’s GUI. This process is known as the GUI. ANS: inflating. 4.4 State whether each of the following is true or false . If false , explain why. a) By default, a Seekbar allows you to select values from 0 to 255. ANS: False. By default, a Seekbar allows you to select values from 0 to 100 . b) A GUI component can span multiple columns in a TableLayout . ANS: True. c) If you’ve installed multiple Android SDKs, the ADT Plugin selects the oldest one as the default for design purposes in the Graphical Layout tab—regardless of the SDK you selected when you created the project. ANS: False. If you’ve installed multiple Android SDKs, the ADT Plugin selects the most recent one as the default for design purposes in the Graphical Layout tab—regardless of the SDK you selected when you created the project. d) Every Activity subclass must override the Construct method. ANS: False. Every Activity subclass must override the onCreate method. e) A paused activity is visible on the screen and has the focus. ANS: False. A paused activity is visible on the screen but doesn’t have the focus. f) Time-consuming initializations should be done in a onCreate method instead of the background process. ANS: False. Time-consuming initializations should be done in a background process instead of the onCreate method . g) You use TableRow s to create the rows in a TableLayout . ANS: True. h) You override the onStart method to initialize the app when it’s launched. ANS: False. You override the onCreate method to initialize the app when it’s launched.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend