ACarter
ACarter

Reputation: 5707

Android string variable always final in R.java

I am building a simple java/android application, and am trying to change the value of a string (which is used as the text of a button) stored in the resources file.

Whenever I try to change the the value (using R.string.), I get an error. This is because in this file (the R.string one), the variable is "final". Whenever I try to edit the file, it reverts it to the original version.

If there a way to stop this string being final, or is there another way to pass values for the text property of something like this?

Upvotes: 0

Views: 937

Answers (3)

Booger
Booger

Reputation: 18725

You are not able to modify the R file directly (it is auto-generated as part of the build process).

If you want to change the label of a button, you could set it in the code like this:

myButton.setText(R.string.myString)

if you would like to change it to a different string, you could always just set it to another predefined String like this:

myButton.setText(R.string.myOtherString)

The R class will automatically make references to Strings from your strings.xml file (which should be in the res\values folder)

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160271

Don't change the value of the string, change the button's text value.

R is auto-generated, don't edit it.

Upvotes: 5

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Resources are static. Period. And R/Class is generated from your xml resource declarations. If you like to store editable values use preferences.

Upvotes: 1

Related Questions