Android Get Store Custom Multiple ArrayList to SharedPreferences Example Tutorial

How to save multiple List string values to SharedPreferences and show them again on activity screen.

In this tutorial we would going to store multiple values to SharedPreferences like an List<String>. This type of feature mostly used where android application developer needs to store different-different type values to into a single object or ArrayList. So here is the  complete step by step tutorial for Android Get Store Custom Multiple ArrayList to SharedPreferences Example Tutorial.

android-project-download-code-button

How to Android Get Store Custom Multiple ArrayList to SharedPreferences Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.arraylistsavesharedpreferences_android_examplescom;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    Button StoreButton, ShowButton;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    TextView textView;

    // Creating List with String Values.
    List<String> CountList = Arrays.asList(
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten"
    );

    // Creating Set.
    Set countSet = new HashSet();

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

        // Opening sharedPreferences in Private mode.
        sharedPreferences = getPreferences(Context.MODE_PRIVATE);

        // Opening sharedPreferences in edit mode using editor.
        editor = sharedPreferences.edit();

        // Assigning ID'S TO buttons and textview .
        StoreButton = (Button) findViewById(R.id.button1);
        ShowButton = (Button) findViewById(R.id.button2);
        textView = (TextView) findViewById(R.id.textview);

        // Adding click listener to Store button .
        StoreButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Calling Method to store values in sharedPreferences.
                StoreListToSharedPreferences();

            }
        });

        // Adding click listener to show button.
        ShowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Calling method to show values from sharedPreferences.
                RetrieveListFromSharedPreferences();

            }
        });
    }

    // Creating method to store values in sharedPreferences.
    public void StoreListToSharedPreferences(){

        countSet.addAll(CountList);

        editor.putStringSet(getResources().getString(R.string.Count_List_Key), countSet);

        editor.apply();

        textView.setText("List Saved in SharedPreferences:-");

        for (String count : CountList){

            textView.setText(textView.getText() + "\n" + count);
        }
    }

    // Creating method to Show values from sharedPreferences.
    public void RetrieveListFromSharedPreferences(){

        Set<String> countsSet = sharedPreferences.getStringSet(getResources().getString(R.string.Count_List_Key), null);

        textView.setText("List Showing from SharedPreferences:-");

        textView.setTextColor(Color.parseColor("#795548"));

        for(String count : countsSet){

            textView.setText(textView.getText() + "\n" + count);
        }


    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.arraylistsavesharedpreferences_android_examplescom.MainActivity"
    android:id="@+id/relativelayout1"
    android:padding="5dp">


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Here To store arraylist to sharedpreferences "
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Here To Show arraylist Stored in sharedpreferences"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/button1"
        android:id="@+id/textview"
        android:textSize="20dp"
        android:textColor="#E91E63"/>

</RelativeLayout>

Code for strings.xml file.

<resources>
    <string name="app_name">ArrayListSaveSharedPreferences-Android-Examples.com</string>
    <string name="Count_List_Key">Count_List_Key</string>
</resources>

Screenshots:-

Android Get Store Custom Multiple ArrayList to SharedPreferences Example Tutorial

Click here to download Android Get Store Custom Multiple ArrayList to SharedPreferences Example Tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *