6

can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.

  String name = getResources().getString(R.string.name);
  if(name.lenght() < 1 ){
      // getResources().setString(R.string.name);??????????????????????

  }

My string.xml is

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <string name="name"></string>
 </resources>

does anybody know how i can add value of name in string.xml though coding. Thank you!!

5 Answers 5

10

The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.

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

Comments

6

I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.

Comments

5

It is not possible to modify the resources of an APK during runtime.

Comments

4

You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.

Comments

4

You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.

Comments

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.