0

I am trying to make my Android application more dynamic. All the strings are in the strings.xml file and retrieved with R.string. Now I would like to fill strings.xml with data from a database. I know how to fetch data and use it, but I don't understand how I can write to the strings.xml. I read that SharedPreferences is one way of retrieving key-value pairs of data, but not sure if that is the best way to go.

Thanks for any suggestions!

4 Answers 4

1

You can't. Resources are precompiled. In strings.xml are stored constant values. Dynamic solution should be based on database.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't write to strings.xml. In is included in your apk file during build time and is not modifiable.

Comments

1

Strings.xml is processed at compile-time, you can't modify it dynamically.

Comments

0

You should try to make two folders of string and from database according to users preference stored in database you should load the strings from that folder.

public static String getResourceString(String name, Context context)
 {
    int nameResourceID = context.getResources().getIdentifier(name, "string", context.getApplicationInfo().packageName);
    if (nameResourceID == 0) {
        throw new IllegalArgumentException("No resource string found with name " + name);
    } else {
        return context.getString(nameResourceID);
    }
} 

1 Comment

hope this helps can't judge the content - but would be more helpful if it were easily readable (or in other words: please edit and format the code :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.