How to change the value of a string in XML?

4

There is a way to change a value of a string in XML.

I know how to get the value through getResource().getString(R.string.value); but I do not know how to change the value directly in XML. Is this allowed? Or are the values created in XML constant (immutable)?

    
asked by anonymous 08.02.2014 / 00:15

1 answer

3

According to this answer in SOEN , what you want is not possible - string.xml is actually only- reading. The recommended alternative is to use SharedPreferences : a means to create and persist user preference data .

This link has an example of how to use them (if this alternative really catches you, of course):

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Recupera as preferências salvas
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // Atribui uma nova preferência; isso é feito através de um Editor
      // Todos os objetos são de android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Confirma as edições
      editor.commit();
    }
}
    
08.02.2014 / 00:21