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>


On Mon, Mar 31, 2008 at 9:48 AM, Ted Hopp <[EMAIL PROTECTED]> wrote:

>
> 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.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to