Android ArrayList of custom objects – Save to SharedPreferences – Serializable?

I have an ArrayList of an object. The object contains the types ‘Bitmap’ and ‘String’ and then just getters and setters for both. First of all is Bitmap serializable?

How would I go about serializing this to store it in SharedPreferences? I have seen many people ask a similar question but none seem to give a good answer. I would prefer some code examples if at all possible.

  • Make image appear half of the screen
  • set focus to input box and showing android keyboard using jquery mobile on pageshow
  • What limits are there on the number of Android resources?
  • Grid Layout support in android API 10
  • Gradle - build sass per productflavor (multi folder)
  • TabLayout update tab content with a custom view
  • If bitmap is not serializable then how do I go about storing this ArrayList?

    many thanks.

    Related posts:

    How to display text with two-color background?
    Add bigger margin to EditText in Android AlertDialog
    Android Google+ SignInButton. How to set size attibute in xml
    actionlayout on menuitem does nothing
    Change DialogFragment enter/exit transition at just before dismissing
    Correct way of calling super.onStop() when unregistering a listener
  • When using the CoordinatorLayout my ScrollView has an incorrect size
  • In Android should each screen be its own Activity or layout?
  • Android Multi-Window Support : Detecting if status bar is visible?
  • How to Make my Android App FullScreen via Android Manifest?
  • How to show an image in the email body?
  • How to round decimal numbers in Android
  • 4 Solutions collect form web for “Android ArrayList of custom objects – Save to SharedPreferences – Serializable?”

    Yes,you can save your composite object in shared preferences.Let say:

     Student mStudentObject=new Student();
     SharedPreferences appSharedPrefs = PreferenceManager
      Editor prefsEditor = appSharedPrefs.edit();
      Gson gson = new Gson();
      String json = gson.toJson(mStudentObject);
      prefsEditor.putString("MyObject", json);

    and now you can retreive your object as:

         SharedPreferences appSharedPrefs = PreferenceManager
          Gson gson = new Gson();
     String json = appSharedPrefs.getString("MyObject", "");
      Student mStudentObject = gson.fromJson(json, Student.class);

    for more information,click here.

    If you want to get back Array List of any type object e.g. Student, then use:

    Type type = new TypeToken<List<Student>>(){}.getType();
    List<Student> students= gson.fromJson(json, type);

    The answer above works, but not for list:

    For saving list of objects do like this:

    List<Cars> cars= new ArrayList<Cars>();
        gson = new Gson();
        String jsonCars = gson.toJson(cars);
        Log.d("TAG","jsonCars = " + jsonCars);

    Read the json object:

    Type type = new TypeToken<List<Cars>>(){}.getType();
    List<Cars> carsList = gson.fromJson(jsonCars, type);

    For Save:

    public static void saveSharedPreferencesLogList(Context context, List<PhoneCallLog> callLog) {
        SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(callLog);
        prefsEditor.putString("myJson", json);

    For load:

    public static List<PhoneCallLog> loadSharedPreferencesLogList(Context context) {
        List<PhoneCallLog> callLog = new ArrayList<PhoneCallLog>();
        SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = mPrefs.getString("myJson", "");
        if (json.isEmpty()) {
            callLog = new ArrayList<PhoneCallLog>();
        } else {
            Type type = new TypeToken<List<PhoneCallLog>>() {
            callLog = gson.fromJson(json, type);
        return callLog;

    PhoneCallLog is my custom object’s name. (Contains String, long and boolean values)

    For me it worked like this :

    Put values in SharedPreferances :

    String key = "Key";
    ArrayList<ModelClass> ModelArrayList=new ArrayList();
    SharedPreferences shref;
    SharedPreferences.Editor editor;
    shref = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = gson.toJson(ModelArrayList);
    editor = shref.edit();
    editor.putString(key, json);

    To get values from SharedPreferances :

    Gson gson = new Gson();
    String response=shref.getString(key , "");
    ArrayList<ModelClass> lstArrayList = gson.fromJson(response, 
        new TypeToken<List<ModelClass>>(){}.getType());
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.