Android: Generic methods to write/save Android preferences data

I was working with Android Preferences recently, and after writing a few “save my preference” methods, I decided to take a few minutes to organize my code and put all of those Preference methods in a PreferenceUtils class.

I made the methods static, and — following functional programming principles — I made their output depend only on their input, so they require you to pass in a Context reference, along with your preference key and value. (Technically the methods aren’t functional because they communicate with the outside world, but at least their behavior relies only on their inputs.)

My Android preferences utility class contains a series of methods to write/save preference values, and those methods look like this:

// string
public static void writePreferenceValue(Context context, String prefsKey, String prefsValue) {
    SharedPreferences.Editor editor = getPrefsEditor(context);
    editor.putString(prefsKey, prefsValue);
    editor.commit();
}

// int
public static void writePreferenceValue(Context context, String prefsKey, int prefsValue) {
    SharedPreferences.Editor editor = getPrefsEditor(context);
    editor.putInt(prefsKey, prefsValue);
    editor.commit();
}

// boolean
public static void writePreferenceValue(Context context, String prefsKey, boolean prefsValue) {
    SharedPreferences.Editor editor = getPrefsEditor(context);
    editor.putBoolean(prefsKey, prefsValue);
    editor.commit();
}

private static SharedPreferences.Editor getPrefsEditor(Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.edit();
}

I just started using these methods, so I’m not aware of any bugs with them, but if I find any bugs I’ll update this code.

The complete source code

If it helps to see the complete source code, including the necessary import statements, here you go:

package com.alvinalexander.preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtils {

    // string
    public static void writePreferenceValue(Context context, String prefsKey, String prefsValue) {
        SharedPreferences.Editor editor = getPrefsEditor(context);
        editor.putString(prefsKey, prefsValue);
        editor.commit();
    }

    // int
    public static void writePreferenceValue(Context context, String prefsKey, int prefsValue) {
        SharedPreferences.Editor editor = getPrefsEditor(context);
        editor.putInt(prefsKey, prefsValue);
        editor.commit();
    }

    // boolean
    public static void writePreferenceValue(Context context, String prefsKey, boolean prefsValue) {
        SharedPreferences.Editor editor = getPrefsEditor(context);
        editor.putBoolean(prefsKey, prefsValue);
        editor.commit();
    }

    private static SharedPreferences.Editor getPrefsEditor(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.edit();
    }
    
}

Summary: Saving Android Preference data

In summary, if you needed to see how to write/save preferences data in an Android application, or otherwise wanted to see a series of preferences utilities like this, I hope this code is helpful.