Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Staggered GridView in Android with Example
Next article icon

Staggered GridView in Android with Example

Last Updated : 29 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. The children are in a staggered grid format.
  2. It supports horizontal and  vertical layout
  3. Example: Pinterest, a wallpaper app, a status app, etc

Staggered GridView

GridView

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

GridView

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:


Next Article
Staggered GridView in Android with Example

O

onlyklohan
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Kotlin
  • Android
  • Technical Scripter 2020
  • Kotlin Android
  • Android-View
  • Java-Android
Practice Tags :
  • Java

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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences