Skip to content

Instantly share code, notes, and snippets.

@layemut
Last active February 13, 2021 13:24
Show Gist options
  • Save layemut/53f71350f2d0f5b982b6e3b8b01c4a7a to your computer and use it in GitHub Desktop.
Save layemut/53f71350f2d0f5b982b6e3b8b01c4a7a to your computer and use it in GitHub Desktop.
Set of methods to save values to and get values from SharedPreferences.

SharedPreferencesUtils

Set of methods to save values to and get values from SharedPreferences.

Set-up

Set context to it before using, for example in Activity's onCreate.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SharedPreferencesUtils.setUpWith(this);
  }

Or even better, set-up when application starts

public class MyAwesomeApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferencesUtils.setUpWith(this);
    }
}

Usage

If you dont specify the location of preferences, it will be default.(your.package.name_prefereces)

        //Save userId and apiKey to user_preferences.xml in SharedPreferences
        SharedPreferencesUtils.save("user_preferences", "userId", "layemut");
        SharedPreferencesUtils.save("user_preferences", "apiKey", API_KEY);

        //Get userId and apiKey saved to user_preferences.xml
        SharedPreferencesUtils.getString("user_preferences", "userId");
        SharedPreferencesUtils.getString("user_preferences", "apiKey");

        //Save intro_done to default SharedPreferences (your.package.name_preferences)
        SharedPreferencesUtils.save("intro_done", true);

        //Get intro_done from default SharedPreferences
        SharedPreferencesUtils.getBoolean("intro_done");

        //Set list of string to browser_preferences.xml in SharedPreferences
        List<String> browserHistory = new ArrayList<>();
        browserHistory.add("facebook.com");
        browserHistory.add("google.com");
        SharedPreferencesUtils.save("browser_preferences", "history", browserHistory);

        //Get list of string from browser_preferences.xml
        SharedPreferencesUtils.getStringList("browser_preferences", "history");
package com.layemut.fieldcontrolledclicklistener.utils;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* Common methods to ease use of {@link SharedPreferences}
*
* @author Özcan Candağ - ozcancandag@gmail.com.
*/
public final class SharedPreferencesUtils {
/**
* Application context
*/
private static Context mContext;
/**
* Setting up with {@link Context} to obtain {@link SharedPreferences}
*
* @param context Application context
*/
public static void setUpWith(Context context) {
mContext = context.getApplicationContext();
}
/**
* Save string value by key to default {@link SharedPreferences}
*
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String key, String value) {
return save(defaultName(), key, value);
}
/**
* Save string value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, String value) {
return save(0, key, value);
}
/**
* Save int value by key to default {@link SharedPreferences}
*
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String key, int value) {
return save(defaultName(), key, value);
}
/**
* Save int value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, int value) {
return save(0, key, value);
}
/**
* Save boolean value by key to default {@link SharedPreferences}
*
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String key, boolean value) {
return save(defaultName(), key, value);
}
/**
* Save boolean value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, boolean value) {
return save(0, key, value);
}
/**
* Save float value by key to default {@link SharedPreferences}
*
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String key, float value) {
return save(defaultName(), key, value);
}
/**
* Save float value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, float value) {
return save(0, key, value);
}
/**
* Save long value by key to default {@link SharedPreferences}
*
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String key, long value) {
return save(defaultName(), key, value);
}
/**
* Save long value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, long value) {
return save(0, key, value);
}
/**
* Save list of string by key to default {@link SharedPreferences}
*
* @param key key to values
* @param values list of strings
* @return if operation succeeds.
*/
public static boolean save(String key, List<String> values) {
return save(defaultName(), key, values);
}
/**
* Save list of string value by key's resId to default {@link SharedPreferences}
*
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int key, List<String> value) {
return save(0, key, value);
}
/**
* Save string value by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String where, String key, String value) {
return getEditor(where).putString(key, value).commit();
}
/**
* Save string value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, String value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putString(getStringByResId(key), value).commit();
}
/**
* Save int value by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String where, String key, int value) {
return getEditor(where).putInt(key, value).commit();
}
/**
* Save int value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, int value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putInt(getStringByResId(key), value).commit();
}
/**
* Save boolean value by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String where, String key, boolean value) {
return getEditor(where).putBoolean(key, value).commit();
}
/**
* Save boolean value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, boolean value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putBoolean(getStringByResId(key), value).commit();
}
/**
* Save boolean value by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String where, String key, float value) {
return getEditor(where).putFloat(key, value).commit();
}
/**
* Save float value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, float value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putFloat(getStringByResId(key), value).commit();
}
/**
* Save long value by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(String where, String key, long value) {
return getEditor(where).putLong(key, value).commit();
}
/**
* Save long value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, long value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putLong(getStringByResId(key), value).commit();
}
/**
* Save list of string by key to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the {@link SharedPreferences} be
* @param key key to values
* @param values list of string
* @return if operation succeeds.
*/
public static boolean save(String where, String key, List<String> values) {
return getEditor(where).putStringSet(key, new HashSet<>(values)).commit();
}
/**
* Save list of string value by key's resId to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the {@link SharedPreferences} be
* @param key resId of key to value
* @param value value to be saved
* @return if operation succeeds.
*/
public static boolean save(int where, int key, List<String> value) {
return getEditor(where == 0 ? defaultName() : getStringByResId(where)).putStringSet(getStringByResId(key), new HashSet<>(value)).commit();
}
/**
* Get string value by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return obtained value by key
*/
public static String getString(String where, String key) {
return getPreference(where).getString(key, "");
}
/**
* Get string value by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return obtained value by key
*/
public static String getString(int where, int key) {
return getPreference(where == 0 ? defaultName() : getStringByResId(where)).getString(getStringByResId(key), "");
}
/**
* Get int value by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return obtained value by key
*/
public static int getInt(String where, String key) {
return getPreference(where).getInt(key, -1);
}
/**
* Get int value by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return obtained value by key
*/
public static int getInt(int where, int key) {
return getPreference(where == 0 ? defaultName() : getStringByResId(where)).getInt(getStringByResId(key), -1);
}
/**
* Get boolean value by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return obtained value by key
*/
public static boolean getBoolean(String where, String key) {
return getPreference(where).getBoolean(key, false);
}
/**
* Get boolean value by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return obtained value by key
*/
public static boolean getBoolean(int where, int key) {
return getPreference(where == 0 ? defaultName() : getStringByResId(where)).getBoolean(getStringByResId(key), false);
}
/**
* Get float value by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return obtained value by key
*/
public static float getFloat(String where, String key) {
return getPreference(where).getFloat(key, -1);
}
/**
* Get float value by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return obtained value by key
*/
public static float getFloat(int where, int key) {
return getPreference(where == 0 ? defaultName() : getStringByResId(where)).getFloat(getStringByResId(key), -1);
}
/**
* Get long value by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return obtained value by key
*/
public static long getLong(String where, String key) {
return getPreference(where).getLong(key, -1);
}
/**
* Get long value by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return obtained value by key
*/
public static long getLong(int where, int key) {
return getPreference(where == 0 ? defaultName() : getStringByResId(where)).getLong(getStringByResId(key), -1);
}
/**
* Get list of string by key saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where where will the value be obtained
* @param key key to value
* @return list of string by key, empty list if not exist
*/
public static List<String> getStringList(String where, String key) {
return new ArrayList<>(getPreference(where).getStringSet(key, new HashSet<String>()));
}
/**
* Get list of string by key's resId saved to {@link SharedPreferences} with name <code>where</code>
*
* @param where resId of where will the value be obtained
* @param key resId of key to value
* @return list of string by key, empty list if not exist
*/
public static List<String> getStringList(int where, int key) {
return new ArrayList<>(getPreference(where == 0 ? defaultName() : getStringByResId(where)).getStringSet(getStringByResId(key), new HashSet<String>()));
}
/**
* Get string value by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return obtained value by key
*/
public static String getString(String key) {
return getString(defaultName(), key);
}
/**
* Get string value by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return obtained value by key
*/
public static String getString(int key) {
return getString(0, key);
}
/**
* Get int value by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return obtained value by key
*/
public static int getInt(String key) {
return getInt(defaultName(), key);
}
/**
* Get int value by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return obtained value by key
*/
public static int getInt(int key) {
return getInt(0, key);
}
/**
* Get float value by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return obtained value by key
*/
public static float getFloat(String key) {
return getFloat(defaultName(), key);
}
/**
* Get float value by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return obtained value by key
*/
public static float getFloat(int key) {
return getFloat(0, key);
}
/**
* Get long value by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return obtained value by key
*/
public static long getLong(int key) {
return getLong(0, key);
}
/**
* Get long value by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return obtained value by key
*/
public static long getLong(String key) {
return getLong(defaultName(), key);
}
/**
* Get boolean value by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return obtained value by key
*/
public static boolean getBoolean(String key) {
return getBoolean(defaultName(), key);
}
/**
* Get boolean value by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return obtained value by key
*/
public static boolean getBoolean(int key) {
return getBoolean(0, key);
}
/**
* Gets list of string by key saved to default {@link SharedPreferences}
*
* @param key key to value
* @return saved list of string by key, empty list if not exist
*/
public static List<String> getStringList(String key) {
return getStringList(defaultName(), key);
}
/**
* Gets list of string by key's resId saved to default {@link SharedPreferences}
*
* @param key resId of key to value
* @return saved list of string by key, empty list if not exist
*/
public static List<String> getStringList(int key) {
return getStringList(0, key);
}
/**
* Removes value from default {@link SharedPreferences}
*
* @param key key to value
*/
public static void remove(String key) {
remove(defaultName(), key);
}
/**
* Removes value from default {@link SharedPreferences}
*
* @param key resId of key to value
*/
public static void remove(int key) {
remove(0, key);
}
/**
* Removes value from {@link SharedPreferences} of <code>where</code>
*
* @param where preference location
* @param key key to value
*/
public static void remove(String where, String key) {
getEditor(where).remove(key);
}
/**
* Removes value from {@link SharedPreferences} of <code>where</code>
*
* @param where resId of preference location
* @param key resId of key to value
*/
public static void remove(int where, int key) {
getEditor(where == 0 ? defaultName() : getStringByResId(where)).remove(getStringByResId(key));
}
/**
* Get {@link SharedPreferences.Editor} of <code>where</code>
*
* @param where where will {@link SharedPreferences.Editor} be of
* @return {@link SharedPreferences.Editor} of <code>where</code>
*/
private static SharedPreferences.Editor getEditor(String where) {
if (mContext == null) {
throw new NullPointerException("Be sure to set up SharedPreferencesUtils");
}
return mContext.getSharedPreferences(where, Context.MODE_PRIVATE).edit();
}
/**
* Get {@link SharedPreferences} of <code>where</code>
*
* @param where where will {@link SharedPreferences} be obtained
* @return {@link SharedPreferences} of <code>where</code>
*/
private static SharedPreferences getPreference(String where) {
if (mContext == null) {
throw new NullPointerException("Be sure to set up SharedPreferencesUtils");
}
return mContext.getSharedPreferences(where, Context.MODE_PRIVATE);
}
/**
* Default name to {@link SharedPreferences}
*
* @return exp: tr.ragnarok.payrov_preferences
*/
private static String defaultName() {
if (mContext == null) {
throw new NullPointerException("Be sure to set up SharedPreferencesUtils");
}
return mContext.getPackageName() + "_preferences";
}
/**
* Returns a localized string from the application's package's
* default string table.
*
* @param resId Resource id for the string
* @return The string data associated with the resource, stripped of styled
* text information.
*/
private static String getStringByResId(int resId) {
if (mContext == null) {
throw new NullPointerException("Be sure to set up SharedPreferencesUtils");
}
return mContext.getString(resId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment