Ch07 - Reveal! - Displaying Pictures in A GridView
Ch07 - Reveal! - Displaying Pictures in A GridView
Using Java, 3E
Chapter 7: Reveal!
Displaying Pictures in a GridView
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 1
or posted to a publicly accessible website, in whole or in part.
Objectives
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 3
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control
• A View container is a rectangular
area of the screen that displays
an image or text
object
• A GridView is a center-locked
horizontally scrolling list
– Adds Visual Appeal
– Clean, Professional Effect
– Flip Photos with Fingers
– Tap for full-size
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 4
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 5
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 6
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 8
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 9
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 10
or posted to a publicly accessible website, in whole or in part.
Adding a GridView Control (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 11
or posted to a publicly accessible website, in whole or in part.
Adding the ImageView Control and
Image Files
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 12
or posted to a publicly accessible website, in whole or in part.
Creating an Array for the Images
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 13
or posted to a publicly accessible website, in whole or in part.
Creating an Array for the Images
(continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 14
or posted to a publicly accessible website, in whole or in part.
Instantiating the GridView and
ImageView Controls
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 15
or posted to a publicly accessible website, in whole or in part.
Instantiating the GridView and
ImageView Controls (Continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 16
or posted to a publicly accessible website, in whole or in part.
Using a setAdapter with an Image Adapter
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 17
or posted to a publicly accessible website, in whole or in part.
Using a setAdapter with an Image Adapter
(Continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 18
or posted to a publicly accessible website, in whole or in part.
Using a setAdapter with an Image Adapter
(Continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 19
or posted to a publicly accessible website, in whole or in part.
Coding the OnItemClickListener
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 20
or posted to a publicly accessible website, in whole or in part.
Coding the OnItemClickListener (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 21
or posted to a publicly accessible website, in whole or in part.
Coding the OnItemClickListener (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 22
or posted to a publicly accessible website, in whole or in part.
Coding the OnItemClickListener (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 23
or posted to a publicly accessible website, in whole or in part.
Coding a Custom Toast Notification
– Recall that toast notifications provide feedback to the
user
• Previous toast notification code:
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 24
or posted to a publicly accessible website, in whole or in part.
Coding a Custom Toast Notification
(continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 26
or posted to a publicly accessible website, in whole or in part.
Displaying the Selected Image
• When the user selects a picture, a toast message
appears and the ImageView control displays the
selected image
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 27
or posted to a publicly accessible website, in whole or in part.
Customizing the ImageAdapter Class
– The ImageAdapter class was called with this line of
code:
• gr.setAdapter(new ImageAdapter (this));
– The ImageAdapter class determines the layout of
the GridView
• The context and images of the GridView need to be
referenced within the ImageAdapter class
– The tasks to complete inside the ImageAdapter
class:
• Manage the layout of the GridView
• Connect the data sources from the array for display
within the GridView control
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 28
or posted to a publicly accessible website, in whole or in part.
Calculating the Length of an Array
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 31
or posted to a publicly accessible website, in whole or in part.
Calculating the Length of an Array (Continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 32
or posted to a publicly accessible website, in whole or in part.
Coding the getView Method
• getView method uses Context to create a new
ImageView instance to temporarily hold each image
displayed in the GridView
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 34
or posted to a publicly accessible website, in whole or in part.
Coding the getView Method (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 35
or posted to a publicly accessible website, in whole or in part.
Coding the getView Method (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 36
or posted to a publicly accessible website, in whole or in part.
Completed Code
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 37
or posted to a publicly accessible website, in whole or in part.
Completed Code (continued)
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 38
or posted to a publicly accessible website, in whole or in part.
Summary
Android Boot Camp for Developers Using Java, 3rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, 40
or posted to a publicly accessible website, in whole or in part.
Summary (continued)