Staggered GridView in Android with Example
Last Updated :
29 Jan, 2025
StaggeredGridLayout is a LayoutManager in the Android studio similar to GridView, but in StaggeredGridLayout each grid has its own height and width.
Difference Between GridView and Staggered GridView
StaggeredGridlayout
- The children are in a staggered grid format.
- It supports horizontal and vertical layout
- Example: Pinterest, a wallpaper app, a status app, etc

GridView
- The children's in a rectangular grid format
- It also supports horizontal and vertical layout
- Example: Flipkart, Amazon, wallpaper app, etc

Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Adding Dependency to the build.gradle File
Go to Module build.gradle file and add this dependency and click on Sync Now button.
implementation ("androidx.recyclerview:recyclerview:1.4.0")
Step 3: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. Add a RecyclerView as shown below.
activity_main.xml:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewStagged"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now we create a new layout resource file (card_item.xml)
Inside it, we add a simple ImageView and set it android:scaleType="fitXY" complete code of recyclerview_row.xml is shown below.
recyclerview_row.xml:
card_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<!-- set "adjustViewBounds" to "true" to prevent images from stretching -->
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/gfg_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Working with the Java/Kotlin Files
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. First, we create a RecyclerViewAdapter class and extend it to RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> and implements its methods as shown below. Below is the code for the RecyclerViewAdapter file. Comments are added inside the code to understand the code in more detail.
RecyclerViewAdapter File:
Java
package org.geeksforgeeks.demo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<Integer> image;
public RecyclerViewAdapter(ArrayList<Integer> image) {
this.image = image;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
viewHolder.imgView.setImageResource(image.get(position));
}
@Override
public int getItemCount() {
return image.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imgView = itemView.findViewById(R.id.imgView);
}
}
}
Kotlin
package org.geeksforgeeks.demo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter(private var image: ArrayList<Int>) :
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val v: View = LayoutInflater.from(viewGroup.context).inflate(R.layout.card_item, viewGroup, false)
return ViewHolder(v)
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
viewHolder.imgview.setImageResource(image[i])
}
override fun getItemCount(): Int {
return image.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imgview: ImageView
init {
imgview = itemView.findViewById(R.id.imgView)
}
}
}
Now Open the MainActivity File there within the Class
First of all, create the object of the RecyclerViewAdapter, RecyclerView class, and an ArrayList to store ids of images
private lateinit var recyclerViewAdapter: RecyclerViewAdapter
private lateinit var imageList: ArrayList<Int>
private lateinit var recyclerView: RecyclerView
Now inside the onCreate() method, link those objects with their respective IDs that are given in the activity_main.xml file.
imageList = ArrayList(mutableListOf(
R.drawable.k1, R.drawable.k2, R.drawable.k3,
R.drawable.k4, R.drawable.k5, R.drawable.k6,
R.drawable.k7, R.drawable.k8, R.drawable.k9
))
recyclerView = findViewById(R.id.recycleViewStagged)
Now inside onCreate() method, we create a RecyclerView.LayoutManager (Learn more about StaggeredGridLayoutManager) and set it to RecyclerView
val layoutManager: RecyclerView.LayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager
Now we initialize and setAdapter()
recyclerViewAdapter = new RecyclerViewAdapter(imageList)
recyclerView.adapter = recyclerViewAdapter
Below is the complete code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
MainActivity File:
Java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private RecyclerViewAdapter recyclerViewAdapter;
private ArrayList<Integer> imageList;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageList = new ArrayList<>(Arrays.asList(
R.drawable.k1, R.drawable.k2, R.drawable.k3,
R.drawable.k4, R.drawable.k5, R.drawable.k6,
R.drawable.k7, R.drawable.k8, R.drawable.k9
));
recyclerView = findViewById(R.id.recycleViewStagged);
RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(
2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(imageList);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var recyclerViewAdapter: RecyclerViewAdapter
private lateinit var imageList: ArrayList<Int>
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// adding values to arrayList
imageList = ArrayList(mutableListOf(
R.drawable.k1, R.drawable.k2, R.drawable.k3,
R.drawable.k4, R.drawable.k5, R.drawable.k6,
R.drawable.k7, R.drawable.k8, R.drawable.k9
))
recyclerView = findViewById(R.id.recycleViewStagged)
// setting recyclerView layoutManager
val layoutManager: RecyclerView.LayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager
recyclerViewAdapter = RecyclerViewAdapter(imageList)
// setting recycle view adapter
recyclerView.adapter = recyclerViewAdapter
}
}
Output:
Similar Reads
GridView in Android with Example
A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView. The main function of th
5 min read
RecyclerView as Staggered Grid in Android With Example
GridView: A ViewGroup that shows the items in a two-dimensional scrolling grid. In Grid View, each grid is of the same size. i.e., the height and width of each grid are equal. It shows symmetric items in the views. Staggered GridView: This ViewGroup is the extension of Grid View. In this view, the G
6 min read
ViewPager2 in Android with Example
Before going to Viewpager2 let us know about Viewpager. Most of you have used WhatsApp, when you open WhatsApp you can see at the top, there are four sections Camera, Chats, Status, Calls these are the Viewpager. So a Viewpager is an android widget that is used to navigate from one page to another p
6 min read
GalleryView in Android with Example
In Android, Gallery is a view that can show items in a center-locked, horizontal scrolling list, and hence the user can able to select a view, and then the user-selected view will be shown in the center of the Horizontal list. "N" number of items can be added by using the Adapter. The adapter is a b
8 min read
Image Switcher in Android with Example
Sometimes, you may not want an image to appear suddenly on the screen. Instead, you might prefer a smooth transition from one image to another using animation. Android offers a tool called ImageSwitcher to help with this. An ImageSwitcher lets you add simple transition effects to your images.What ar
4 min read
ViewSwitcher in Android with Example
All android apps will have a feature to switch different views in order to explain/promote their site or product. Visual representation of a product by showing different views will impress the customers easily. In this article, let us see how to bring the "ViewSwitcher" to Android. ViewSwitcher is a
6 min read
GridView Using BaseAdapter in Android with Example
Here, we are designing an Android app to demonstrate the use of GridView layout. The GridView layout is a ViewGroup that groups view in a two-dimensional scrolling grid of rows and columns. Items in the grid view are inserted using a ListAdapter. The layout by default is scrollable and we don't need
6 min read
ViewStub in Android with Example
In Android, ViewStub is a zero-sized invisible View and very flexible in that we can lazily inflate layout resources at runtime. It is a dumb and lightweight view and it has zero dimension. Depending upon the requirement, whenever in need we can inflate at runtime. This is a classic example to handl
6 min read
Staggered GridView in Android using Jetpack Compose
Staggered Grid View has been seen in most applications such as Pinterest in which each item of gridview takes its own height and aligns within the grid view according to that. In this article, we will look at how to implement Staggered Grid View in Android using Jetpack ComposeStep by Step Implement
4 min read
ViewAnimator in Android with Example
ViewAnimator is a very fascinating and useful feature as it switches between two or more views smoothly and mainly meant for animation features of the views on screens. It is the parent class of ViewFlipper and ViewSwitcher and the main distinction is it can switch between more than 2 views also. It
4 min read