android: assigning a constant value to a xml-element
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"/>
It's possible using Data Binding.
The class should look something like this:
class Constants extends BaseObservable { private static final int MY_INT = 10; @Bindable public int getMyInt() { return MY_INT; }}
and the xml like this:
<layout ... > <data> <variable name="constants" type="your.package.Constants" /> </data>...<YourComponent android:max='@{constamts.myInt}'...</layout>
- Don't forget to set the binding itself (
binding.setConstants(constants)
).
Your question is not very clear, what do you want to do with that?If you want to use a constant value then just create the ui-widgets with the constants and just ignore the xml.