Chapter 5 Investigate Android Lists Arrays and Web Browsers 1 Android Boot Camp for Developers Using Java 2nd Ed Objectives In this chapter you learn to Create an Android project using a list ID: 635243
Download Presentation The PPT/PDF document "Android Boot Camp for Developers Using J..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android AppsChapter 5: Investigate! Android Lists, Arrays, and Web Browsers
1
Android Boot Camp for Developers Using Java, 2nd Ed.Slide2
ObjectivesIn this chapter, you learn to:Create an Android project using a listDevelop a user interface that uses ListViewExtend the ListActivity classUse an array to create a listCode a setListAdapter to display an arrayDesign a custom ListView layout with XML code
Display an image with the ListView controlChange the default title bar text
2
Android Boot Camp for Developers Using Java, 2nd Ed.Slide3
Objectives (continued)Code a custom setListAdapter for a custom layoutCall the onListItemClick method when a list item is selectedWrite code using the Switch decision structureCall an intent to work with an outside appOpen an Android Web browserLaunch a Web site through the use of a URI using an Android browserTest an application with multiple
decisions
3
Android Boot Camp for Developers Using Java, 2nd Ed.Slide4
Creating a ListLists are one of the most common designs in mobile appsScrollableSelectableProgrammable to bring up thenext Activity (screen)
4
Android Boot Camp for Developers Using Java, 2nd Ed.Slide5
Creating a List5Android Boot Camp for Developers Using Java, 2nd Ed.Slide6
Creating a List (continued)Steps to complete the app:Create a list using a ListView control.Define an array to establish the items of the list.
Add the images used in the project.Define an XML file to design the custom list with a leading image.
Code a switch decision structure to handle the selection of items.
Open an Android Web browser to display a specified Uniform Resource Identifier (URI).
Create multiple classes and XML layout files to display pictures of attractions.
6
Android Boot Camp for Developers Using Java, 2nd Ed.Slide7
Creating a List (continued)Opening screen contains a vertical list of attractionsList is automatically scrollable if it exceeds the window sizeListView is better than TableLayout View because each row can be selected for further actionSelecting an item opens up a related Web page or an image of the attraction
7
Android Boot Camp for Developers Using Java, 2nd Ed.Slide8
Creating a List (continued)8Android Boot Camp for Developers Using Java, 2nd Ed.
Extending a ListActivity
A
ListActivity
class is needed to
display
a list of
items
An ExpandedlistView can be
used to provide a two-level
listSlide9
Creating a List (continued)9Android Boot Camp for Developers Using Java, 2nd Ed.Slide10
Creating a List (continued)Creating an ArrayArray variables can store more than one valueDifferent from other data types that can hold only one valueEach individual item in an array is called an elementRefer to each element using an index in the array
10
Android Boot Camp for Developers Using Java, 2nd Ed.Slide11
Creating a List (continued)Declaring an ArraySquare brackets [ ] are used to define an arrayCurley braces { } contain the list of items in the array
String[] attraction={"Alcatraz Island", "Ferry Marketplace", "
Golden Gate Bridge", "Cable Car Trolley", "Fisherman's Wharf
"};
11
Android Boot Camp for Developers Using Java, 2nd Ed.Slide12
Creating a List (continued)Using a setListAdapter and Array AdapterAn adapter provides a model for the layout and converts the data into a listThe setListAdapter
connects the list items to the images or Web pages they representAn array adapter supplies the array data to the ListView
setListAdapter(new
ArrayAdapter <
String
> (this,android.R.layout.simple_list_item_1
, attraction));
12
Android Boot Camp for Developers Using Java, 2nd Ed.Slide13
Creating a List (continued)Adding the Images to the Resources FolderImages must be locatedin the drawable-hdpi folderRemember that images
may be subject to copyright laws
13
Android Boot Camp for Developers Using Java, 2nd Ed.Slide14
Creating a List (continued)Changing the Title Bar TextCustom titles can appear in the title bar at the top of the window
14
Android Boot Camp for Developers Using Java, 2nd Ed.Slide15
Creating a List (continued)Creating a Custom XML Layout for ListView
15
Android Boot Camp for Developers Using Java, 2nd Ed.Slide16
Creating a List (continued)Creating a Custom XML Layout for ListView (Continued)
16Android Boot Camp for Developers Using Java, 2nd Ed.Slide17
Creating a List (continued)Coding a setListAdapter with a Custom XML LayoutBuilt-in layout is called simple_list_item_1
17
Android Boot Camp for Developers Using Java, 2nd Ed.Slide18
Creating a List (continued)Using the onListItemClick methodonListItemClick() is called when an item from the list is selectedThe item’s position in the list is captured so the app knows which of the items was selected
The position represents the number of the item in the list
18
Android Boot Camp for Developers Using Java, 2nd Ed.Slide19
Creating a List (continued)Using the onListItemClick method (Continued)
19
Android Boot Camp for Developers Using Java, 2nd Ed.Slide20
Creating a List (continued)Decision Structure – Switch StatementIf statements are also decision structuresThe switch statement is used when there are many list items to be evaluatedCan only evaluate integers or single characters
The keyword case is used to test each itemThe keyword break
is used to exit the switch decision structure
20
Android Boot Camp for Developers Using Java, 2nd Ed.Slide21
Creating a List (continued)Decision Structure – Switch Statement (Continued)
21
Android Boot Camp for Developers Using Java, 2nd Ed.Slide22
Creating a List (continued)Android IntentsAndroid intents send and receive activities and services including:Opening a Web pageCalling a phone numberLocating a GPS position on a mapPosting notes to a note-taking program
Opening your contacts listPosting to a social network
22
Android Boot Camp for Developers Using Java, 2nd Ed.Slide23
Creating a List (continued)Launching the Browser from an Android DeviceThe intent sends the browser a URI (Uniform Resource Identifier)URI is similar to URL (Uniform Resource Locator)
URI has additional information necessary for gaining access to the resources required for posting the pageThe action called ACTION_VIEW
(must be in
caps
) is what actually displays the page in the browser
ACTION_VIEW is the most common action performed on data
23
Android Boot Camp for Developers Using Java, 2nd Ed.Slide24
Creating a List (continued)24Android Boot Camp for Developers Using Java, 2nd Ed.Slide25
Designing XML Layout FilesXML layout files must be designed to display an ImageView control with an image source file 25
Android Boot Camp for Developers Using Java, 2nd Ed.Creating a List
(continued)Slide26
Designing XML Layout Files (Continued)26Android Boot Camp for Developers Using Java, 2nd Ed.
Creating a List (continued)Slide27
Designing XML Layout Files (Continued)27Android Boot Camp for Developers Using Java, 2nd Ed.
Creating a List (continued)Slide28
Adding Multiple Class FilesA new class is needed to display each imageClasses must be referenced in the Android Manifest FileCode for Wharf.java class (Bridge and trolley already created)
28
Android Boot Camp for Developers Using Java, 2nd Ed.
Creating a List
(continued)Slide29
Adding Multiple Class Files (Continued)29Android Boot Camp for Developers Using Java, 2nd Ed.
Creating a List
(continued)Slide30
Opening the Class FilesstartActivity opens the next Activity which launches the appropriate XML layoutRunning and testingthe ApplicationBe sure to test every optionShould handle
all clicks
30
Android Boot Camp for Developers Using Java, 2nd Ed.
Creating a List
(continued)Slide31
SummaryThe Java View class creates a list and makes it scrollable; use a ListView control to select each row for further actionExtend the ListActivity class in Main.java to display a ListView controlDeclare list items in an array variableThe index provides access to each element in the list (the list begins with 0)To declare an array, specify the data type followed by the values
31
Android Boot Camp for Developers Using Java, 2nd Ed.Slide32
Summary (continued)Use an adapter to display the values in the arrayA ListView control is a container for the list items and the adapter binds the elements of the array to the ListView layout statements if the condition is falseDrag controls from the palette to the emulator for a simple designAdd code to the main.xml file to get a custom layout
32
Android Boot Camp for Developers Using Java, 2nd Ed.Slide33
Summary (continued)App names are displayed in the app title bar – can be customizedsetListAdapter has 3 parameters:this classThe layout used to display the listThe array containing the list valuesCode the onListItemClick method to respond to the event of the user’s selection
Use the switch decision structure with a list or menu
33
Android Boot Camp for Developers Using Java, 2nd Ed.Slide34
Summary (continued)Android intents send and receive activities and servicesTest every possible combination of clicks, including incorrect user entries before publishing the app
34Android Boot Camp for Developers Using Java, 2nd Ed.