I have searched around and I found good examples on adding custom
boolean states to a drawable but I am having issues adding a state
that is a list of enums.  I am sure most of my problems lie within the
onCreateDrawableState call so I have a few questions.

1) Is the extraSpace param to onCreateDrawableState the number of
additional int[] that I would like to add or the size of the int[]? So
since I am just adding MODE I should just be increasing extraSpace by
1 and not by MODE.length?
2) I don't quite understand the mergeDrawableStates method.  If I put
it in an IF statement I am getting what I expect, but I think I am
just getting lucky.  I don't feel my IF check is correct.  My initial
assumption was that I don't do a check and always call
mergeDrawableStates but then the drawable never changes.  I have seen
where if the extra state is a boolean then the if statement checks for
that.  I don't quite know what to do about an enum.
3) When I do get it working, my ic_button_a is drawn correctly.
However every time when ic_button_c is getting drawn, ic_button_c is
resized and stretch to fit the View which includes a background
graphic that is larger than ic_button_c.  ic_button_a and ic_button_a
are the same size.

I have my custom view:
        <com.pkg.CustomView
            android:background="@drawable/ic_button_bg"
            android:src="@drawable/ic_button"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

which extends ImageButton.:
public class CustomView extends ImageButton {

    private static final int[] MODE = {R.attr.mode};
    private int mMode = 1;

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState =
super.onCreateDrawableState(extraSpace + 1);
        if (mMode != 0) {
            mergeDrawableStates(drawableState, MODE);
        }
        return drawableState;
    }

    public void setMode(int mode) {
        mMode = mode;
        refreshDrawableState();
    }
}

I am defining my modes here:
<resources>
    <declare-styleable name="CaptureButton">
        <attr name="mode">
            <enum name="a"  value="0" />
            <enum name="b"  value="1" />
            <enum name="c"  value="2" />
            <enum name="d"  value="3" />
            <enum name="e"  value="4" />
            <enum name="f"   value="5" />
            <enum name="g"  value="6" />
            <enum name="h"  value="7" />
        </attr>
    </declare-styleable>
</resources>

My @drawable/ic_button is:
<selector xmlns:android="http://schemas.android.com/apk/res/android";
    xmlns:app="http://schemas.android.com/apk/res/com.pkg";>
    <item app:mode="c"  android:drawable="@drawable/ic_button_c" />

    <item android:drawable="@drawable/ic_button_a" />
</selector>

-- 
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]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to