Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android SharedPreferences Utility Class
SharedPrefUtils.java can be used to access android SharedPreferences in easy way. Typically created for android's learners to reduce their
code but there is no many difference in getting preferences values.
////////////////////////////
To put value in preferences.
////////////////////////////
SharedPrefUtils.init(this, "my_pref")
.put("int_pref", 1203)
.put("float_pref", 1.203f)
.put("bool_pref", true)
.put("str_pref", "String Pref")
.put("long_pref", 100000000000099L)
.put("set_pref", setObject)
.finish();
////////////////////////////////////////////////////////////////////
To get value in preferences, use static method myPref(contex, name).
////////////////////////////////////////////////////////////////////
SharedPrefUtils.myPrfe(context, "my_pref").getFloat("float_pref", 0.0f);// assume context as a Context object.
/////////////////////////////////
To remove a value in preferences.
/////////////////////////////////
SharedPrefUtils.init(this, "my_pref").remove(keyToRemove).finish();
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.Map;
import java.util.Set;
/**
* A utility class for android {@link SharedPreferences}.
* All the creation of {@link SharedPreferences} use
* MODE_PRIVATE constant of {@link Context} class.
* Created on 2/4/2018
*
* @author Nyein Chan Aung
* @version 1.0
*/
public final class SharedPrefUtils {
private SharedPreferences sharedPref;
private SharedPreferences.Editor editor;
/**
* A private constructor and initialize a {@link SharedPreferences} and
* a {@link SharedPreferences.Editor}.
*
* @param context Context
* @param name {@link SharedPreferences}'s name
*/
private SharedPrefUtils(Context context, String name) {
sharedPref = context.getSharedPreferences(name, Context.MODE_PRIVATE);
editor = sharedPref.edit();
}
/**
* Create an instance of this class. Create a new instance whenever this
* method is called.
*
* @param context Context
* @param name {@link SharedPreferences}'s name
* @return SharedPrefUtils object
*/
public static SharedPrefUtils init(@NonNull Context context, @NonNull String name) {
return new SharedPrefUtils(context, name);
}
/**
* This method return a {@link SharedPreferences} object with given
* name. The main purpose of this method is to use all getX() method of
* {@link SharedPreferences} class. You can also create a {@link SharedPreferences}
* and use in normal way.
*
* @param context
* @param name
* @return {@link SharedPreferences}
*/
public static SharedPreferences myPref(@NonNull Context context, @NonNull String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
/**
* Put value to preferences depend on data type.
*
* @param k key
* @param v value
*/
public SharedPrefUtils put(String k, Object v) {
if (v instanceof Boolean) {
editor.putBoolean(k, (Boolean) v);
} else if (v instanceof Integer) {
editor.putInt(k, (Integer) v);
} else if (v instanceof Integer) {
editor.putInt(k, (Integer) v);
} else if (v instanceof String) {
editor.putString(k, (String) v);
} else if (v instanceof Float) {
editor.putFloat(k, (Float) v);
} else if (v instanceof Long) {
editor.putLong(k, (Long) v);
} else try {
editor.putStringSet(k, (Set<String>) v);
} catch (ClassCastException e) {
e.printStackTrace();
}
return this;
}
/**
* Remove a preference value of given string key
* if exist.
*
* @param key String, key to remoe
* @return SharedPrefUtils
*/
public SharedPrefUtils remove(String key) {
editor.remove(key);
return this;
}
/**
* Commit Preferences changes and clear all data.
*
* @return boolean true on success, false on failure
*/
public boolean finish() {
if (sharedPref != null && editor != null) {
boolean flag = editor.commit();
sharedPref = null;
editor = null;
return flag;
}
return false;
}
/**
* Show info of the preference with the name provided
* in init method. The value will get from toString() method.
*/
public void log() {
Map<String, ?> map = sharedPref.getAll();
for (Map.Entry<String, ?> entry : map.entrySet()) {
Log.i("info - " + getClass().getName() + " : ", entry.getKey() + " : " + entry.getValue().toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.