Android Advance Image Gallery Example

json, image url loading, nested image views, PlaceHolderView

Before Moving on to this tutorial I would request you to go through the Android Beginner Image Gallery Example.

Objectives Outline for this tutorial:

  1. We would be building a list of images with two types of views. One on the top with horizontal image list and below with vertical image list and apply animations on these view items as shown in the above display image.
  2. We will be loading the images from urls and setting it in the view display. For this purpose we will be using a library Glide.
  3. The Urls list will be seeded in the application and this seed json file will be stored in the assets folder.
  4. The seed file will be parsed into Image object using another library gson.
  5. This structure will also be compatible if we are pulling url json data from a live server.

Let’s start building:

Step 1:

Set up the project in android studio with default activity.

In app’s build.gradle add the dependencies.

Notes:

  1. Add an assets folder in the main directory and point to it in gradle assets.srcDirs
  2. CardView is used to display the image in the list

Add Internet permission in the app’s AndroidManifest.xml

Step 2:

Create src/layout/activity_main.xml

Step 3:

Create src/layout/activity_item_big.xml

Step 4:

Create src/layout/activity_item_small.xml

Step 5:

Create src/layout/activity_item_small_list.xml

Step 6:

Place images.json file in the assets folder created in the above step 1.

Notes:

  1. This strategy is very useful in bundling app with seed files. Seed files contain data build in the app package and can be used to populate database or used to display default data to the user. Placing seed files in the form of json makes is extremely easy to parse into models.

Step 7:

Create Utils.java

Note:

  1. Utils contain the methods required to parse seed json file and also populate the model Image.java

Step 8:

Create model Image.java

Notes:

  1. @SerializedName annotation belongs to gson class and used to read json file variable and bind it to the model variable.
  2. @Expose is used to make the variable readable to the gson

Step 9:

We will now create the class to bind the item views and its operations.

Create ImageTypeBig.java

Notes:

  1. @NonReusable is annotation in PlaceHolderView, to be used in cases where we want to release all the resources and references if removed from the list and won’t use the same object in addView() method in PlaceHolderView.
  2. @layout is used to bind the layout with this class
  3. @View is used to bind the views in this layout we want to refer to
  4. @Resolve is used to operate on the view references obtained from @View, in short if we want to define any operation on the view it should be put in a method and annotated with @Resolve
  5. @Animate() is used to assign defined animations in the Animation class
  6. @LongClick is used to attach the long click listener and the item is removed from the list on event in this case

For detailed explanations view PlaceHolderView at GitHub repository

Step 10:

Create ImageTypeSmall.java

Step 11:

Create ImageTypeSmallList.java

Note:

  1. This class is required to create a nested list view
  2. The Nesting is done by using the PlaceHolderView inside main view
  3. The PlaceHolderView is instantiated and views are added to it.

Step 12:

Create MainActivity.java

Notes:

  1. We obtain the instance of the PlaceHolderView and add views using the url from the model list.
  2. Top 10 images are added to top list
  3. Rest are added to bottom view in reverse order.

PlaceHolderView GitHub repository is here

The source code for this example is here

That’s All

Coders Rock!!