Search code examples
javaandroiddrawable

Android: Adding a Drawable to an existing LayerDrawable


I have a LayerDrawable which I construct with an array of Drawables of 5 Drawables. Now let’s say in run-time I want to add another Drawable to my LayerDrawable, in response to an event. How do I do that without having to re-create the LayerDrawable, this time with an array of Drawables of 6 Drawables? Thanks.


Solution

  • After LayerDrawable is created, new Drawables can not be added to it.

    See the source of LayerDrawable: array of drawables is saved in mLayerState.mChildren and is only set in Constructor.

    However, setDrawableByLayerId(..) can be used to exchange an existing Drawable with a new one.