Building Dynamic Image Galleries in Android Using Java: A Comprehensive Tutorial

Jun 4, 2024

Follow us on


Dive into Android app development with our step-by-step guide on creating versatile image galleries using Java. Learn how to implement dynamic image grids, handle image loading efficiently, and create immersive user experiences in your Android apps.

Gallery in Android

Building Dynamic Image Galleries in Android Using Java: A Comprehensive Tutorial


FOLLOW THESE STEPS


Step 1: res > layout > activity_main.xml

	
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">


    <androidx.cardview.widget.CardView
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:cardCornerRadius="150dp"
        android:layout_marginTop="30dp"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:src="#686C6B"
            android:scaleType="fitXY"
            android:id="@+id/imgGallery"
            />
    </androidx.cardview.widget.CardView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnGallery"
        android:layout_marginTop="21dp"
        android:text="@string/openGallery"
        />

</LinearLayout>

Step 2: MainActivity.class



	package com.heycolleagues.gallery;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private final int GALLERY_REQ_CODE = 1000;
    ImageView imgGallery;
    Button btnGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imgGallery = findViewById(R.id.imgGallery);
        btnGallery = findViewById(R.id.btnGallery);

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent iGallery = new Intent(Intent.ACTION_PICK);
                iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(iGallery, GALLERY_REQ_CODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode == RESULT_OK){
            if (requestCode == GALLERY_REQ_CODE){
                imgGallery.setImageURI(data.getData());
            }
        }


    }
}


© 2025 Hey Colleagues. All rights reserved.