title: How to store theme on SharedPreference in Android
body: <p>I'm trying to save my theme by using sharedpreference, so I used this code:</p>
  
  <p>My SettingsActivity:</p>
  
  <pre><code>public class SettingsActivity extends Activity implements OnClickListener {
  
  
  @Override
  
  public void onCreate(Bundle savedInstanceState) 
  
  {
  
  super.onCreate(savedInstanceState);
  
  ThemeUtils.onActivityCreateSetTheme(this);
  
  setContentView(R.layout.settings);
  
  findViewById(R.id.blackbutton).setOnClickListener(this);
  
  findViewById(R.id.bluebutton).setOnClickListener(this);
  
  findViewById(R.id.pinkbutton).setOnClickListener(this);
  }
  public void onClick(View v) 
  
  {
  
  switch (v.getId())
  
  {
  case R.id.blackbutton:
  
      ThemeUtils.changeToTheme(this, ThemeUtils.BLACK);
  
      break;
  
      case R.id.bluebutton:
  
      ThemeUtils.changeToTheme(this, ThemeUtils.BLUE);
  
      break;
  
      case R.id.pinkbutton:
  
          ThemeUtils.changeToTheme(this, ThemeUtils.PINK);
  
          break;
  
      }
  
      }
  }
  </code></pre>
  
  <p>My Sharedpreference:</p>
  
  <pre><code> public class SharedPreferencesManager {
  
  private SharedPreferences sPreferences;
  
   private SharedPreferences.Editor sEditor;
  
  
  private Context context;
  
  public SharedPreferencesManager(Context context){
  this.context = context;
  sPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  }
  
  private SharedPreferences.Editor getEditor(){
  return sPreferences.edit();
  }
  
  public void storeBoolean(String tag, boolean value){
  sEditor = getEditor();
  sEditor.putBoolean(tag,value);
  sEditor.commit();
  }
  
  public void storeString(String tag, String str){
  sEditor = getEditor();
  sEditor.putString(tag, str);
  sEditor.commit();
  }
  
  public boolean retrieveBoolean(String tag, boolean defValue){
  return sPreferences.getBoolean(tag,defValue);
  
  }
  
  public String retrieveString(String tag, String defStr){
  return sPreferences.getString(tag, defStr);
  }
  
  public int retrieveInt(String tag, int defValue){
  return sPreferences.getInt(tag, defValue);
  }
  public void storeInt(String tag, int defValue){
  sEditor = getEditor();
  sEditor.putInt(tag, defValue);
  sEditor.commit();
  }
  }
  </code></pre>
  
  <p>This is my ThemeUtils:</p>
  
  <pre><code>public class ThemeUtils
  
  {
  
  private static int cTheme;
  
  
  
  public final static int BLACK = 0;
  
  public final static int BLUE = 1;
  public final static int PINK = 2;
  
  public static void changeToTheme(Activity activity, int theme)
  
  {
  
  cTheme = theme;
  
  activity.finish();
  
  
  
  activity.startActivity(new Intent(activity, activity.getClass()));
  
  
  }
  
  public static void onActivityCreateSetTheme(Activity activity)
  
  {
  
  switch (cTheme)
  
  {
  
  default:
  
  case BLACK:
  
  activity.setTheme(R.style.BlackTheme);
  
  break;
  
  case BLUE:
  
  activity.setTheme(R.style.BlueTheme);
  
  break;
  
  case PINK:
  
  activity.setTheme(R.style.PinkTheme);
  
  break;
  
  }
  
  }
  
  }
  </code></pre>
  
  <p>If I close the App and restart them the theme will reset. When the user clicks a button with a certain theme, I want that theme to be set as default and saved so when they reopen the app, it's still that new theme.</p>