Skip to content

Instantly share code, notes, and snippets.

@shirou
Created October 9, 2010 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirou/618164 to your computer and use it in GitHub Desktop.
Save shirou/618164 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class GlobalSetting {
public static int deviceWidth;
public static int deviceHeight;
public static final String FILL_COLUMN = "fillColumn";
public static final String TAB_WIDTH = "tabWidth";
public static int tabWidth;
public static int fillColumn;
public static void savePreference(Context context, String key, Object value){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
if (value instanceof Boolean){
editor.putBoolean(key, (Boolean)value);
}else if (value instanceof String){
editor.putString(key, (String)value);
}else if (value instanceof Integer){
editor.putInt(key, (Integer)value);
}else if (value instanceof Float){
editor.putFloat(key, (Float)value);
}else if (value instanceof Long){
editor.putLong(key, (Long)value);
}
editor.commit();
}
/**
Load default preferences
it should be called from onResume().
*/
public static void loadPreferences(Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
tabWidth = prefs.getInt(TAB_WIDTH, 8);
fillColumn = prefs.getInt(FILL_COLUMN, 72);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment