Android Image Gallery HorizontalScrollView Tutorial

Gallery widely used in displaying a list of thumbnails image but Gallery class was depreciated in API 16 which is Android version 4.1. So, we will need to use another widget to fully replace gallery widget. The best solution to solve this problem is using horizontalScrollView, it very simple to implement in the activity. HorizontalScrollView is a frame layout and scroll item horizontally which different with ScrollView in vertical scrolling. You should place one child inside the horizontal scroll view such as linear layout. In this tutorial, I will give you some steps to use the horizontal scroll view in the Android activity.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “HorizontalSVExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml layout

Go to activity_main.xml edit your XML to below source code. I add horizontal scroll view and linear layout in this XML.

Add a new xml

Create a new xml file by go to res > right click layout > new > layout resource file. Name it to activity_gallery_item.xml then click ok buton.

Edit activity_gallery_item.xml layout

This customizes the item layout in the horizontal scrollview. So u can inflate the xml later.

Add an image in drawable

Add one image to your drawable package, just copy and paste one image file to your drawable package.

Edit class

Follow below source code and write to This class is to add the view to the horizontalscrollview.

Run your project

Now, you can run your project in your emulator or android device.

(Android Image Gallery HorizontalScrollView)

Source Code


(Visited 16,141 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

3 Responses

  1. silpa says:

    i want from gallery images not from drawble .can any one help me pls. thanks in advance.

  2. swapna says:

    This tutorial helped me a lot.clear explanation, thank you.

  3. Thanks – I converted this quick snippet to Kotlin for a project I am working on.
    Awesome. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *