There's precious little documentation about the declare-styleable
tag by which we can declare custom styles for components. I did find this list of valid values for the format
attribute of the attr
tag. While that's nice as far as it goes, it doesn't explain how to use some of those values. Browsing attr.xml (the Android source for the standard attributes), I discovered that you can do things like:
<!-- The most prominent text color. -->
<attr name="textColorPrimary" format="reference|color" />
The format
attribute can evidently be set to a combination of values. Presumably the format
attribute helps the parser interpret an actual style value. Then I discovered this in attr.xml:
<!-- Default text typeface. -->
<attr name="typeface">
<enum name="normal" value="0" />
<enum name="sans" value="1" />
<enum name="serif" value="2" />
<enum name="monospace" value="3" />
</attr>
<!-- Default text typeface style. -->
<attr name="textStyle">
<flag name="normal" value="0" />
<flag name="bold" value="1" />
<flag name="italic" value="2" />
</attr>
Both of these seem to declare a set of allowed values for the indicated style.
So I have two questions:
- What's the difference between a style attribute that can take on one of a set of
enum
values and one that can take on a set offlag
values? - Does anyone know of any better documentation for how
declare-styleable
works (other than reverse engineering the Android source code)?
There's this question here: Defining custom attrs with some info, but not much.
And this post . It has good info about flags and enums:
As I see it, the real values you can add in reality to an attribute is limited by what you can obtain from it. Check the
AttributeSet
class reference here for more hints.You can obtain:
getAttributeBooleanValue
),getAttributeFloatValue
),getAttributeIntValue
),getAttributeUnsignedIntValue
),getAttributeValue
)@Aleadam 's answer is very helpful, but imho it omits one major difference between
enum
andflag
. The former is intented for us to pick one, and only one value when we assign the corresponding attribute for some View. The latter's values can be combined, however, using the bitwise OR operator.An example, in
res/values/attr.xml
In
res/layout/mylayout.xml
we can now doSo an enum selects one of its possible values, while flags can be combined. The numerical values should reflect this difference, typically you'll want the sequence to go
0,1,2,3,...
for enums (to be used as array indices, say) and flags to go1,2,4,8,...
so they can be independently added or removed, using bitwise OR|
to combine flags.We could explicitly define "meta flags" with values that are not a power of 2, and thus introduce a kind of shorthand for common combinations. For instance, if we had included this in our
myflags
declarationthen we could have written
myflags="three"
in stead ofmyflags="one|two"
, for completely identical results as3 == 1|2
.Personally, I like to always include
which will allow me to unset or set all flags at once.
More subtly, it might be the case that one flag is implied by another. So, in our example, suppose that the
eight
flag being set should force thefour
flag to be set (if it wasn't already). We could then re-defineeight
to pre-include, as it were, thefour
flag,Finally, if you are declaring the attributes in a library project but want to apply them in layouts of another project (dependent on the lib), you'll need to use a namespace prefix which you must bind in the XML root element. E.g.,