VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-save-arraylist-to-sharedpreferences-in-android/

⇱ How to Save ArrayList to SharedPreferences in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Save ArrayList to SharedPreferences in Android?

Last Updated : 23 Jul, 2025

SharedPreferences in Android is local storage that is used to store strings, integers, and variables in phone storage so that we can manage the state of the app. We have seen storing simple variables in shared prefs with key and value pair. In this article, we will see How we can store ArrayList to shared preferences in our Android app.  

What we are going to build in this article?

We will be building a simple application in which we will be displaying course names with its description in a simple RecyclerView and we will be storing all this data in our shared preferences. So when we close our app and reopens our app all the data will be saved in shared preferences and the state of our recycler view is maintained. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Adding dependency for gson in build.gradle

Navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.

implementation 'com.google.code.gson:gson:2.8.5'

After adding this dependency sync your project. 

Step 3: Creating a modal class for storing our data

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name your class as CourseModal and add the below code to it. 

Step 4: Creating a layout file for our item of RecyclerView

Navigate to the app > res > layout > Right-click on it > New > layout resource file and name your layout as course_rv_item and add the below code to it. 

Step 5: Creating an adapter class for setting data to items of our RecyclerView 

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseAdapter and add the below code to it. 

Step 6: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

Step 7: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Now run your app and see the output of the app. 

Output:

Comment
Article Tags:

Explore