This week we are going to create a simple photo album to see how displaying images works in Android. It will have a next and a back button. Every time we click next, we will increase an array value and show the next photo. Once we reach the end, it should go back to the start.
First off, we need a place to store our images. Inside Android Studio, this is done inside of the /res/ folder. There are multiple folders for different image resolutions.
Xhdpi 96px (Xtra-high dpi)
Hdpi 72px (High dpi)
Mdpi 48px (Medium dpi)
Ldpi 36px (Low dpi)
Start off by putting 9 or so images into the /res/drawable-mdpi folder and naming them img1.jpg … img9.jpg.
Next create a activity_gallery layout with a TableLayout that will hold our two buttons, prevButton and nextButton, plus an ImageView. The ImageView is a container that will display the image on screen.
To create a simple array of the names of our images, create an xml file named arrays.xml inside the /res/values/ folder.
The @drawable is a reference to the drawable folder. It doesn’t matter that they images are inside drawable-mdpi, the system will know where to find them.
Create an activity named GalleryActivity. Here we will tell the buttons what to do, create an array to track which images to display, and display them on the screen.