Skip to content

Instantly share code, notes, and snippets.

@krupalshah
Last active June 7, 2017 15:58
Show Gist options
  • Save krupalshah/ed011aa73ab465e67f22fb3dca23a0ac to your computer and use it in GitHub Desktop.
Save krupalshah/ed011aa73ab465e67f22fb3dca23a0ac to your computer and use it in GitHub Desktop.
helper for shared prefs - java version
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
/**
* helper for shared prefs - java version
*/
public class PreferenceHelper {
private PreferenceHelper() {}
/**
* Helper method to retrieve a String value from {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return The value from shared preferences, or null if the value could not be read.
*/
public static String getStringPreference(Context context, String key) {
String value = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getString(key, null);
}
return value;
}
/**
* Helper method to write a String value to {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return true if the new value was successfully written to persistent storage.
*/
public static boolean setStringPreference(Context context, String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null && !TextUtils.isEmpty(key)) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
return false;
}
/**
* Helper method to retrieve a float value from {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
public static float getFloatPreference(Context context, String key, float defaultValue) {
float value = defaultValue;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getFloat(key, defaultValue);
}
return value;
}
/**
* Helper method to write a float value to {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return true if the new value was successfully written to persistent storage.
*/
public static boolean setFloatPreference(Context context, String key, float value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat(key, value);
return editor.commit();
}
return false;
}
/**
* Helper method to retrieve a long value from {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
public static long getLongPreference(Context context, String key, long defaultValue) {
long value = defaultValue;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getLong(key, defaultValue);
}
return value;
}
/**
* Helper method to write a long value to {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return true if the new value was successfully written to persistent storage.
*/
public static boolean setLongPreference(Context context, String key, long value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putLong(key, value);
return editor.commit();
}
return false;
}
/**
* Helper method to retrieve an integer value from {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return The value from shared preferences, or the provided default.
*/
public static int getIntegerPreference(Context context, String key, int defaultValue) {
int value = defaultValue;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getInt(key, defaultValue);
}
return value;
}
/**
* Helper method to write an integer value to {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return true if the new value was successfully written to persistent storage.
*/
public static boolean setIntegerPreference(Context context, String key, int value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
return false;
}
/**
* Helper method to retrieve a boolean value from {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
public static boolean getBooleanPreference(Context context, String key, boolean defaultValue) {
boolean value = defaultValue;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getBoolean(key, defaultValue);
}
return value;
}
/**
* Helper method to write a boolean value to {@link SharedPreferences}.
*
* @param context a {@link Context} object.
* @return true if the new value was successfully written to persistent storage.
*/
public static boolean setBooleanPreference(Context context, String key, boolean value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment