user1629977
user1629977

Reputation: 437

How to change R.String.... values in runTime

I have a static values

  <integer-array name="accordion_visibility">
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>1</item>
    <item>0</item>
    <item>0</item>
    <item>0</item>
</integer-array>

I am trying to change this values in run time. but unable to do it

Upvotes: 0

Views: 614

Answers (3)

bendaf
bendaf

Reputation: 3031

You cannot modify strings.xml in runTime. If you want to store some strings on the phone's storage use SharedPreferences. This is how I usually use it:

public class Preferences {

    // Constant key values
    public static final String SERVER_IP  = "Server"; 
    // {....}
    public static final String SOUND = "Notif sound";


    // Required classes for SharedPreferences
    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;

    public Preferences(Context context) {
        this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
        this.editor = this.sharedPreferences.edit();
    }

    /**
     * Set a new value in the shared preferences.
     * <p>
     * You can get the value by the get function.
     * @param key   key of the value, one of the constant strings of {@link Preferences}'s.
     * @param value the value, that should be stored.
     *
     * @see     #get(String, String)
     *
     */
    public void set(String key, String value) {
        this.editor.putString(key, value);
        this.editor.commit();
    }

    /**
     * Get the value of a previously set data if exact or return the default value if not.
     * @param key           key of the value, one of the constant strings of {@link Preferences}'s.
     * @param defaultValue  the default value, return this if the key is not in the database.
     * @return              the value belongs to the key or the default value.
     *
     * @see     #set(String, String)
     */
    public String get(String key, String defaultValue) {
        return this.sharedPreferences.getString(key, defaultValue);
    }

    /**
     * Removes a key value pair from the database.
     * @param key   The key of the pair which should be removed.
     */
    public void clear(String key) {
        this.editor.remove(key);
        this.editor.commit();
    }

    /**
     * Delete all key value pairs from the database.
     */
    public void clear() {
        Log.d(TAG,"SharedPreferences cleared");
        this.editor.clear();
        this.editor.commit();
    }
}

Upvotes: 0

Arturo
Arturo

Reputation: 548

You can't and it's not the good approach trying to do it. You always can retrieve them, use them and modify them if it's necessary and save them if you need save the state. You can use SharedPreferences to save data between executions easily.

Upvotes: 3

RAAAAM
RAAAAM

Reputation: 3380

You can't change strings.xml dynamically since it's a compiled resource. But you can use the sharedPreference to change the values dynamically.

Upvotes: 2

Related Questions