24
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail Flow Activity on a Tablet 1 Android Boot Camp for Developers Using Java, 2nd Ed.

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Embed Size (px)

Citation preview

Page 1: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First

Android Apps

Chapter 9: Customize! Navigating with a Master/Detail Flow Activity on a Tablet

1Android Boot Camp for Developers Using Java, 2nd Ed.

Page 2: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Objectives

In this chapter, you learn to:• Understand responsive design for Android apps• Create an Android tablet project using an application

template• Understand the Master/Detail Flow template• Modify the Master/Detail Flow template• Add a WebView widget

2Android Boot Camp for Developers Using Java, 2nd Ed.

Page 3: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Objectives (continued)

• Display a Web browser within a tablet app• Add an Internet permission to the Android Manifest• Customize the content of the sample template file• Display a custom layout in the details pane

3Android Boot Camp for Developers Using Java, 2nd Ed.

Page 4: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Understanding Responsive Design

• Responsive design – An approach to

designing apps and Web sites that provide an optimal viewing experience across as many devices as possible

4Android Boot Camp for Developers Using Java, 2nd Ed.

Page 5: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Using Application Templates

• Application templates– Used to create basic Android applications that can

immediately be run and test on an Android device of any size

– Android templates are available when you create a new Android project

5Android Boot Camp for Developers Using Java, 2nd Ed.

Page 6: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Using Application Templates (Continued)

Understanding the Structure of the Master/Detail Flow Template

– Several Java and XML layout resource files have been created automatically

6Android Boot Camp for Developers Using Java, 2nd Ed.

Page 7: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Using Application Templates (Continued)

SRC Files– ItemDetailActivity.java

• The purpose of this class is to display the activity_item_detail.xml layout file if a smartphone is detected

– ItemDetailFragment.java • The purpose of this class is to display the fragment_item_detail.xml layout

file– ItemListActivity.java

• The purpose of this initial Activity is to display the master list in a two-pane mode if a tablet device is detected or to launch a second Activity to display the detailed items if a smaller device is detected

– ItemListFragment.java• The purpose of this class is to display the activity_item_list.xml layout file

– DummyContent.java• The purpose of this Java file is to provide sample code you customize to

suit your specific app content.

7Android Boot Camp for Developers Using Java, 2nd Ed.

Page 8: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Using Application Templates (Continued)

Layout Files– activity_item_detail.xml

• When a smartphone is detected, the app uses this layout to display the FrameLayout instance

– activity_item_list.xml• When a smartphone is detected, the app uses this layout to

display the master list fragment– activity_item_twopane.xml

• When a tablet is detected, the app is displayed in a two-pane layout containing both the master item list fragment and the item detail container

– fragment_item_detail.xml• When a smartphone or a tablet is detected, this layout file

displays the detail pane using the onCreateView( ) method8Android Boot Camp for Developers Using Java, 2nd Ed.

Page 9: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Using Application Templates (Continued)

9Android Boot Camp for Developers Using Java, 2nd Ed.

• Adding Images to the Drawable Folder– If necessary, expand the src and

layout folders in the Package Explorer

– Drag the image files to the drawable-hdpi folder until a plus sign pointer appears

Page 10: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Designing an XML TableLayout

• The Strings table is responsible for the text displayed in the app– The first string is displayed in the title bar of the

tablet, and the second string is displayed in the title bar of a smaller device such as a smartphone

10Android Boot Camp for Developers Using Java, 2nd Ed.

Page 11: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Designing an XML TableLayout (continued)

11Android Boot Camp for Developers Using Java, 2nd Ed.

Page 12: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Designing an XML TableLayout (continued)

12Android Boot Camp for Developers Using Java, 2nd Ed.

Page 13: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Designing an XML TableLayout (continued)

13Android Boot Camp for Developers Using Java, 2nd Ed.

Page 14: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a TextView XML Layout for the Second List Item

14Android Boot Camp for Developers Using Java, 2nd Ed.

Page 15: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

15Android Boot Camp for Developers Using Java, 2nd Ed.

• Webview– A View widget

object that displays Web pages

• Permission– A restriction limiting access to a part of the code or

to data on the device

Creating a WebView XML Layout for the Third List Item

Code Syntaxandroid:permission="android.permission.INTERNET"

Page 16: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a WebView XML Layout for the Third List Item (continued)

16Android Boot Camp for Developers Using Java, 2nd Ed.

Page 17: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a WebView XML Layout for the Third List Item (continued)

17Android Boot Camp for Developers Using Java, 2nd Ed.

Page 18: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a WebView XML Layout for the Third List Item (continued)

• 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, and 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

18Android Boot Camp for Developers Using Java, 2nd Ed.

Page 19: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a WebView XML Layout for the Third List Item (continued)

19Android Boot Camp for Developers Using Java, 2nd Ed.

Page 20: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Creating a WebView XML Layout for the Third List Item (continued)

20Android Boot Camp for Developers Using Java, 2nd Ed.

Page 21: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Running and Testing the Application

• Tap or click Run on the menu bar• Select Android Application from the dialog box• Save all the files when prompted• Unlock the emulator (if necessary)• Test each tab to ensure it works as intended

– You must have Internet connectivity to open the Web page and enough memory available to handle the app connection to the Web

21Android Boot Camp for Developers Using Java, 2nd Ed.

Page 22: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Summary

• Responsive design is an approach to designing apps and Web sites that provide an optimal viewing experience across as many devices as possible

• Android templates are available when you create a new Android project and are used to create basic Android applications that you can run and test on an Android device of any size

• A tablet app created with the Master/Detail Flow template displays a Master list of items in a narrow vertical pane along the left edge of the screen

• To display the detail content for the first list item in the Bike and Barge app, you provide images and text descriptions in an XML TableLayout. Each row in the TableLayout displays an ImageView and TextView control

22Android Boot Camp for Developers Using Java, 2nd Ed.

Page 23: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Summary (continued)

• To display the detail content for the second list item, you code two TextView controls in a Linear layout that include the tour details

• To display the detail content for the third list item, you customize the default fragment_item_detail.xml layout to use a WebView object instead of a TextView object

• After including a WebView object in the XML layout of an app, you must add the INTERNET permissions to the Android Manifest file so the app can access the Internet and load Web pages

23Android Boot Camp for Developers Using Java, 2nd Ed.

Page 24: Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 9: Customize! Navigating with a Master/Detail

Summary (continued)

• To associate the content displayed in the detail pane with each list item in the left pane, you customize the DummyContent.java class file by adding code to the addItem statement so it references three String objects: the id, the item list string, and the Web site URL

• To handle the responses to user selections, you add conditional statements to the ItemDetailFragment.java file

24Android Boot Camp for Developers Using Java, 2nd Ed.