0

I have strings stored in an XML file for my app (for easier changes and translations, etc). My question is if there is anyway to take a value from the table and set it in a compile time constant variable.

For example:

public static final String foo = /*grab string from resources*/    

Right now I have this:

public static final String PROTO = Resources.getSystem().getString(R.string.stage_proto);    

which when I later try to use it for a case label in a switch-case statement, I get an error saying it's not a constant expression.

Is there anyway to set a value from Resources to a compile time constant variable?

JMekker
  • 160
  • 1
  • 8
  • You would need to generate Java code from your XML with a string constant, and then refer to the generated code from your hand-written class. – Andy Turner Jul 20 '16 at 21:29
  • Why is changing an XML file easier than changing a Java file? They are both just text files. If the value needs "translation", it is probably not a good candidate for a `switch` statement. – Andreas Jul 20 '16 at 21:31
  • I dont think so, but if you are using gradle you might be do [>this<](http://stackoverflow.com/questions/17197636/is-it-possible-to-declare-a-variable-in-gradle-usable-in-java) – einschneidend Jul 20 '16 at 22:37

1 Answers1

-1

It is not possible. Because in android xml files are compile time files so once apk is generated you can not change strings from xml files

  • I don't want to change the string. I just want a reference to it that is compile time constant. – JMekker Jul 21 '16 at 18:01