I'm currently programming an android app. There I am stuck with xml-layout. Is it possible to assign a value to an xml-tag using a variable (constant) defined in a class?
i have a class called Constants.java for all my program constants. i do this for better maintainability. now i would like to use one of this constants, e.g. VAL as defined below as value for an ui-widged.
public class Constants {
public static final int VAL = 10;
...
}
in my case, the widget is a progressbar (horizontal style) and i would like to define the "android:max" value. normally one can write:
android:max="10"
android:max="@Integer/val
but i would like to use the value defined in my Constants class, something like:
android:max="Constants.VAL"
is there a solution for that?
thanks
No, you can't. Constant values in classes are only available at runtime, and the XML files are compiled and generated before runtime.
The next-best thing to do is declare the XML constants you want to use in res/values/integers.xml
. Here's an example integers.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="max">10</integer>
</resources>
to use this value in your XML, do this:
<YourComponent
android:yourattr="@integer/max"/>