Code example for StateListDrawable

Methods: setBoundssetState

0
    } 
 
    public void setState(int [] state) {
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            d.setState(state);
        } 
    } 
/* 
    public boolean hasState(int [] state) { 
        if (mDrawable instanceof StateListDrawable) { 
            StateListDrawable d = (StateListDrawable) mDrawable; 
            // TODO: this doesn't seem to work 
            return d.getStateDrawableIndex(state) != -1; 
        } 
        return false; 
    } 
*/ 
    /** 
     * Returns true if the drawable is a StateListDrawable and is in the focused state. 
     * 
     * @return 
     */ 
    public boolean isActive() { 
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
            int[] states = d.getState();
            for (int i = 0; i < states.length; i++) {
                if (states[i] == android.R.attr.state_focused) {
                    return true; 
                } 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Returns true if this target is enabled. Typically an enabled target contains a valid 
     * drawable in a valid state. Currently all targets with valid drawables are valid. 
     * 
     * @return 
     */ 
    public boolean isEnabled() { 
        return mDrawable != null && mEnabled;
    } 
 
    /** 
     * Makes drawables in a StateListDrawable all the same dimensions. 
     * If not a StateListDrawable, then justs sets the bounds to the intrinsic size of the 
     * drawable. 
     */ 
    private void resizeDrawables() { 
        if (mDrawable instanceof StateListDrawable) {
            StateListDrawable d = (StateListDrawable) mDrawable;
//            int maxWidth = 0; 
//            int maxHeight = 0; 
//            int[] state = d.getState(); 
//            for (int i = 0; i < state.length; i++) { 
//                Drawable childDrawable = d..getStateDrawable(i); 
//                maxWidth = Math.max(maxWidth, childDrawable.getIntrinsicWidth()); 
//                maxHeight = Math.max(maxHeight, childDrawable.getIntrinsicHeight()); 
//            } 
//             
//            if (DEBUG) Log.v(TAG, "union of childDrawable rects " + d + " to: " 
//                        + maxWidth + "x" + maxHeight); 
//            d.setBounds(0, 0, maxWidth, maxHeight); 
//            for (int i = 0; i < d.getState(); i++) { 
//                Drawable childDrawable = d.getStateDrawable(i); 
//                if (DEBUG) Log.v(TAG, "sizing drawable " + childDrawable + " to: " 
//                            + maxWidth + "x" + maxHeight); 
//                childDrawable.setBounds(0, 0, maxWidth, maxHeight); 
//            } 
//             
            d.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        } else if (mDrawable != null) {
            mDrawable.setBounds(0, 0,
                    mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        } 
    }