Search code examples
androidimagepicker

How to display a group of images from drawable to let the user pick one (Android Studio)?


I currently have an activity with a horizontal scrollview, and inside it I have an add button (ImageView) to let the user add some images to the same horizontal scrollview after clicking it. How can I display a group of images from app resources (such as drawable) as a sort of "pop up" to let the user pick one, without changing the current activity?


Solution

  • This can be solved by inflating a custom xml layout (containing a GridView) on an AlertDialog. To display the images on the GridView you need to create and set a custom adapter to it. The adapter shall have a function getView(), which will return each of the Views to be shown on Grid cells. You can also use Picasso Library to load the images.

    Here is a brief tutorial

    http://www.101apps.co.za/articles/gridview-tutorial-using-the-picasso-library.html

    Hint: Remember to define the number of columns of the grid, and calculate the size of each image based on the screen size. This way you can fit the amount you like on each row. You can do this for both portrait and landscape orientations by overriding the function onConfigurationChanged.

    Hint 2: Remember a GridView already has the scroll property, which needs to be activated (android:scrollbars="horizontal"). Using a ScrollView as parent of a GridView will cause it to wrap the image, even with MATCH_PARENT property activated, causing layout problems.