Rakel He
Rakel He

Reputation: 51

iterate the child drawable of StateListDrawable in android

In my current project,I need to iterate each of the child drawable in StateListDrawable.

In the source code of the StateListDrawable.java, I find a method:

public Drawable getStateDrawable(int index)

but the annotation of this method is @hide, which means I can not use it.

So, is there any alternative way to achieve this?

Upvotes: 5

Views: 582

Answers (1)

Squatting Bear
Squatting Bear

Reputation: 1724

This is a fairly old question now, so it's probably too late to help you, but for the sake of anyone else who finds it I was able to do it with this code:

final DrawableContainer parentDrawable = (DrawableContainer) drawable;
final DrawableContainerState containerState = (DrawableContainerState) parentDrawable.getConstantState();
final Drawable[] children = containerState.getChildren();
for (int i = containerState.getChildCount(); i > 0; --i) {
    doStuff(children[i - 1]);
}

Upvotes: 5

Related Questions