2

The documentation of the XML resource specification of LayerList and LevelList drawables says that the item element will only accept bitmap as child element. On some answers concerning level lists, I see that people are adding shape tags as children to the item tags. I tried out the following and it seems to work nicely.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:maxLevel="0">
    <shape android:shape="rectangle">
      <solid android:color="#ff0000" />
    </shape>
  </item>
  <item android:maxLevel="1">
    <shape android:shape="oval">
      <solid android:color="#0000ff" />
    </shape>
  </item>
</level-list>

Here is my question. Is this documented anywhere, or should I avoid this kind of construct because it is not supported in all API versions, or is the documentation simply incomplete and the above is perfectly legal?

To clarify, I know that I can specify any sort of drawable by using separate resource files and referring to them using the android:drawable attribute. The question refers to the inline specification of arbitrary drawables within the XML of the layer list or level list.

Community
  • 1
  • 1

2 Answers2

1

It doesn't state that it only accepts Bitmap objects, but that it will accept them. You can use the android:drawable to specify a drawable. Pull your shapes into their own drawable XML definition and reference them in the level-list.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • I know that I can specify any kind of drawable using the `android:drawable` attribute. My question refers to the XML specification. Can I specify shapes (or other drawables for that matter) inline inside the item tag. his means that I don't create multiple resources for every level or layer. – mikail sheikh Jan 08 '14 at 17:56
  • You're right in that the documentation isn't exactly clear, other than it states that any child of will work. – Larry Schiefer Jan 08 '14 at 20:30
  • No, that's not quite correct. In the documentation of `` it is stated that `` "must be a child of a `` element." This is clearly wrong because `` defines a `StateListDrawable` and `` defines a `LevelListDrawable`. The documentation also states that `` "accepts child `` elements". While this is true, it is incomplete. Apparently it can accept child `` elements as well. I just wanted to know if it is safe for me to do so or if there might be some incompatibilities on some API versions. – mikail sheikh Jan 08 '14 at 20:47
0

The answer is YES, it is allowed and supported behaviour.

While it is not documented, non-bitmap tags are used in the official Android XML resources. For example the source of progress_large_holo.xml contains the following.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
             android:drawable="@drawable/spinner_76_outer_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="0"
             android:toDegrees="1080" />
    </item>
    <item>
        <rotate
             android:drawable="@drawable/spinner_76_inner_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="720"
             android:toDegrees="0" />
    </item>
</layer-list>

Other sources also reveal that any XML that is accepted as a drawable resource can be placed inside the item tags.