1

I have a strings.xml file that contains pre-defined lists. I want the user to have control over those lists. In other words, the user should be able to change the content of the pre-defined list, is there a way in android-studio/java to achieve that?

I tried to see if I could change the file content in my MainActivity but without success.

Is there a way to dynamically make pre-defined lists of content editable if it's impossible?

Any help or thoughts would be appreciated!

moken
  • 3,227
  • 8
  • 13
  • 23
amine ech
  • 23
  • 2

1 Answers1

0

How I would go about this is to first have a shared preferences, with all the optional user defined values. Here is a helper class that can give you an idea of what I'm talking about. (Documentation for Shared Preferences here: https://developer.android.com/reference/android/content/SharedPreferences)

public class UserDefinedValuesHelper {

public static String VALUES = "VALUES";

// Define your values as Static String for easy access
public static String EXAMPLEVALUEKEYNAME = "EXAMPLEVALUEKEYNAME";

    public UserDefinedValuesHelper (Context context) {
        definedValues = context.getSharedPreferences(VALUES, MODE_PRIVATE);
    }

    public void saveValueString(String key, String value) {
        SharedPreferences.Editor editor = definedValues.edit();
        editor.putString(key, value);
        editor.apply();
    }

    //Create getters for all your values
    public String getExampleKeyName() {
        return definedValues.getString(EXAMPLEVALUEKEYNAME, R.string.examplestring);
//the first parameter of definedValues.getString is the Key. If it is null than the second parameter is the default value and is returned.

    }


}

Examples of using the helper in another class

 //instantiate userDefinedValuesHelper at top of class
 UserDefinedValuesHelper userDefinedValuesHelper;

 //define userDefinedValuesHelper onCreate/initialize of class
 userDefinedValuesHelper = new UserDefinedValuesHelper(this);


//Saving to userDefinedValuesHelper
userDefinedValuesHelper.saveValueString(userDefinedValuesHelper.EXAMPLEVALUEKEYNAME, "myStringValueHere");

//Getting String value
String myStringValue = userDefinedValuesHelper.getExampleKeyName();
JonR85
  • 700
  • 4
  • 12
  • Thank for the answer, can I store arrays of strings too using SharedPreferences or just simple strings ? – amine ech May 02 '23 at 14:10
  • It does look like you can. https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences. But maybe you can share some of your code, so that I can get a better idea of what you are attempting. – JonR85 May 02 '23 at 14:28
  • the problem is that I am just guthering information about wether it can be done or not, I am just a beginner in android studio, thanks anyway ! – amine ech May 03 '23 at 06:42
  • If you found the information I proved helpful please consider upvoting or marking this answer as the solution. – JonR85 May 03 '23 at 19:58