meaning of format reference in android attribute

Nouvel Travay picture Nouvel Travay · Jul 29, 2016 · Viewed 8k times · Source

Let's start with an example

<attr name="spinnerDropDownItemStyle" format="reference" />

How do I take this apart and understand it?

I watch the Android themes & styles demystified - Google I/O 2016 talk multiple times and I still don't understand how this reference thing works.

Answer

frogatto picture frogatto · Jul 29, 2016

reference format is used when the attribute refers to another resource ID. For example:

<!-- Declaration -->
<attr name="mydrawale" format="reference" />

<!-- Usage -->
app:mydrawable="@drawable/shape1"

So, resources like @layout/..., @color/..., @style/... are of type reference. However attribute formats like color, boolean, etc need also color literals (e.g. 0x00FF00) and boolean literals (e.g. true) respectively.

In your case, spinnerDropDownItemStyle is an attribute that gets a resource like @style/... which by itself is a pointer to a resource entry of type style.

By the way, you can set the format to multiple types using |. For example for the following attribute both @color/mycolor and 0x00FF00 are acceptable.

<attr name="mycolor" format="reference|color" />