Custom drawing states possible?

Showing 1-5 of 5 messages
Custom drawing states possible? Ted Hopp 3/31/08 9:48 AM
With some searching I've found several examples and explanations of
customizing the look of a component based on the drawing states. But I
haven't found anything about defining custom drawing states. For
instance, suppose I have a component that maintains states that I
might call "baked" and "fried" and I want it's appearance to track
these states. So I'm imagining this drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>

Is this possible? I see that it is possible (with setState,
mergeDrawableStates, etc.) to have custom state data, but I don't see
how to put this to use. I can't figure out from the documentation
whether it is even possible to define custom item state attributes in
xml, much less how to do it and link them to the state vector. (I'm
assuming that if this is possible, I could also mix custom attributes
with standard ones like android:state_pressed.) A pointer to an
example or two would be very helpful.

Thanks.
Re: [android-developers] Custom drawing states possible? Megha 3/31/08 4:22 PM
Hi,

You can use the mergeDrawableStates() method of the View class to provide your custom states:

http://code.google.com/android/reference/android/view/View.html#mergeDrawableStates(int[],%20int[])

Try the steps below to create new custom states:

1) Define the state resources in res/values/attrs.xml

   <declare-styleable name="MyCustomState">
        <attr name="state_fried" format="boolean" />
        <attr name="state_baked" format="boolean" />
    </declare-styleable>

2) In your custom view class :

A)   Declare the state variables:

   private static final int[] FRIED_STATE_SET = {
        R.attr.state_fried
   };
   
   private static final int[] BAKED_STATE_SET = {
        R.attr.state_baked
   };


B) Override the onCreateDrawableState() method:

   @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
        if (isFried()) {
            mergeDrawableStates(drawableState, FRIED_STATE_SET);
        }
        if (isBaked()) {
            mergeDrawableStates(drawableState, BAKED_STATE_SET);
        }
        return drawableState;
    }

Once this is done, you should be able to use these in ColorStateListDrawable, but you should use your app's namespace to use these new states:


<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/<my_app_package>">

<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>


Re: Custom drawing states possible? Ted Hopp 3/31/08 8:04 PM
Thanks! That's very cool.


On Mar 31, 6:22 pm, "Megha Joshi" <mjo...@google.com> wrote:
> Hi,
>
> You can use the mergeDrawableStates() method of the View class to provide
> your custom states:
>
> http://code.google.com/android/reference/android/view/View.html#merge...[],%20int[])
Re: [android-developers] Custom drawing states possible? Mustafa Ali 11/6/12 3:29 AM
Tried using it on 4.0.3, the onCreateDrawableState method is not getting called.
Re: [android-developers] Custom drawing states possible? Alik Elzin 2/6/14 7:58 AM
Try adding an onclicklistener and invoke refreshDrawableState from performClick.