2

Here is my code:

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Editor ed=prefs.edit();
ed.putString(getString(R.string.firsttemplate), "String Modified");
ed.apply();
ed.commit();
Toast.makeText(getBaseContext(), getString(R.string.firsttemplate), Toast.LENGTH_SHORT).show();

this Toast is showing the previous text saved in my string value

I also saw already asked questions about SharedPreferences but it wasn't helpful.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205

1 Answers1

1

You can't change resource files during runtime. Strings are hard-coded in the string.xml file and hence can't be changed during runtime. Instead of trying to edit your strings.xml file, just use SharedPreferences to store the user's preferences if that's what you're trying.

You can use this code is basis for you saving and restoring values from SharedPreferences.

public class Account {

private static Account account;
private static final String ACCESS_TOKEN = "access_token";
public String accessToken;

public static Account getInstance() {
    if (account == null)
        account = new Account();
    return account;
}

public void save(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    Editor editor = prefs.edit();

    editor.putString(ACCESS_TOKEN, accessToken);

    editor.commit();
}

public void restore(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    accessToken = prefs.getString(ACCESS_TOKEN, accessToken);
}

private Account() {

}
}

Now you can access your values like this. Restoring:

Account account = Account.getInstance();
account.restore(getActivity());
Toast.makeText(getActivity(), account.accessToken, Toast.LENGTH_SHORT).show();

Saving:

Account account = Account.getInstance();
account.accessToken = "newString";
account.save(getActivity());
Anatol
  • 941
  • 1
  • 7
  • 13
  • actually I wanna change the title of menuitem after editing.. the modification is okay, but when I restart my app the value is again changed to the default value of the string. – Zeeshan Khalid Jan 05 '15 at 08:10
  • okay, so if you know how to setTitle() to some specific menuitem, please tell me. – Zeeshan Khalid Jan 05 '15 at 08:17
  • Now your string is saving inside accessToken of your Account Instance. You can restrore and saving it whith `context` instance. Activity is context, so you can pass it as parameter. http://developer.android.com/training/basics/data-storage/shared-preferences.html Here is one more example of using sharedPrefences. Is it clear now? – Anatol Jan 05 '15 at 08:20
  • in the link you shared, they are also changing the value of String in strings.xml, then how can you say that resource file can't be modified.. I've tried that code before you shared, but my code is inside onClick() method, and it's giving me errors :P – Zeeshan Khalid Jan 05 '15 at 08:27
  • @ZeeshanKhalid No, you are misunderstanding) First parameter in this expression: `ed.putString(getString(R.string.firsttemplate), "String Modified");` is just a key for saving value "String Modified". So lately you can access your value in SharedPrefences by key "getString(R.string.firsttemplate)". – Anatol Jan 05 '15 at 08:33
  • R.string.firsttemplate is the reference to a string"firsttemplate" in my string.xml. and this line should be modifying that value, this is written in the link you shared.. :P – Zeeshan Khalid Jan 05 '15 at 09:24
  • I've added you.. hope the matter will be solved on skype.. Thank you – Zeeshan Khalid Jan 05 '15 at 09:39