Example usage for android.graphics.drawable Drawable setState

List of usage examples for android.graphics.drawable Drawable setState

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable setState.

Prototype

public boolean setState(@NonNull final int[] stateSet) 

Source Link

Document

Specify a set of states for the drawable.

Usage

From source file:com.hippo.widget.recyclerview.EasyRecyclerView.java

private void positionSelector(int position, View sel, boolean manageHotspot, float x, float y) {
    final boolean positionChanged = position != mSelectorPosition;
    if (position != INVALID_POSITION) {
        mSelectorPosition = position;/*from   w w  w  .j av a 2  s .  c  o m*/
    }

    final Rect selectorRect = mSelectorRect;
    selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());

    // Adjust for selection padding.
    selectorRect.left -= mSelectionLeftPadding;
    selectorRect.top -= mSelectionTopPadding;
    selectorRect.right += mSelectionRightPadding;
    selectorRect.bottom += mSelectionBottomPadding;

    // Update the selector drawable.
    final Drawable selector = mSelector;
    if (selector != null) {
        if (positionChanged) {
            // Wipe out the current selector state so that we can start
            // over in the new position with a fresh state.
            selector.setVisible(false, false);
            selector.setState(StateSet.NOTHING);
        }
        selector.setBounds(selectorRect);
        if (positionChanged) {
            if (getVisibility() == VISIBLE) {
                selector.setVisible(true, false);
            }
            updateSelectorState();
        }
        if (manageHotspot) {
            DrawableUtils.setHotspot(selector, x, y);
        }
    }
}

From source file:android.support.v7.widget.SearchView.java

private void updateCloseButton() {
    final boolean hasText = !TextUtils.isEmpty(mSearchSrcTextView.getText());
    // Should we show the close button? It is not shown if there's no focus,
    // field is not iconified by default and there is no text in it.
    final boolean showClose = hasText || (mIconifiedByDefault && !mExpandedInActionView);
    mCloseButton.setVisibility(showClose ? VISIBLE : GONE);
    final Drawable closeButtonImg = mCloseButton.getDrawable();
    if (closeButtonImg != null) {
        closeButtonImg.setState(hasText ? ENABLED_STATE_SET : EMPTY_STATE_SET);
    }//from ww w .  j  a  v  a 2  s .  co  m
}

From source file:android.support.v7.widget.SearchView.java

private void updateFocusedState() {
    final boolean focused = mSearchSrcTextView.hasFocus();
    final int[] stateSet = focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET;
    final Drawable searchPlateBg = mSearchPlate.getBackground();
    if (searchPlateBg != null) {
        searchPlateBg.setState(stateSet);
    }//from   w ww .j a  va2  s  .c  o  m
    final Drawable submitAreaBg = mSubmitArea.getBackground();
    if (submitAreaBg != null) {
        submitAreaBg.setState(stateSet);
    }
    invalidate();
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java

private void setSpecialKeysIconsAndLabels() {
    Key enterKey = findKeyByPrimaryKeyCode(KeyCodes.ENTER);
    if (enterKey != null) {
        enterKey.icon = null;//w w w.ja  v  a  2 s .  c o m
        enterKey.iconPreview = null;
        enterKey.label = null;
        ((AnyKey) enterKey).shiftedKeyLabel = null;
        Drawable icon = getIconToDrawForKey(enterKey, false);
        if (icon != null) {
            enterKey.icon = icon;
            enterKey.iconPreview = icon;
        } else {
            CharSequence label = guessLabelForKey(enterKey.getPrimaryCode());
            enterKey.label = label;
            ((AnyKey) enterKey).shiftedKeyLabel = label;
        }
        // making sure something is shown
        if (enterKey.icon == null && TextUtils.isEmpty(enterKey.label)) {
            Logger.i(TAG, "Wow. Unknown ACTION ID " + mKeyboardActionType + ". Will default to ENTER icon.");
            // I saw devices (Galaxy Tab 10") which say the action
            // type is 255...
            // D/ASKKbdViewBase( 3594): setKeyboardActionType
            // imeOptions:33554687 action:255
            // which means it is not a known ACTION
            Drawable enterIcon = getIconForKeyCode(KeyCodes.ENTER);
            enterIcon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_NORMAL);
            enterKey.icon = enterIcon;
            enterKey.iconPreview = enterIcon;
        }
    }
    //these are dynamic keys
    setSpecialKeyIconOrLabel(KeyCodes.MODE_ALPHABET);
    setSpecialKeyIconOrLabel(KeyCodes.MODE_SYMOBLS);
    setSpecialKeyIconOrLabel(KeyCodes.KEYBOARD_MODE_CHANGE);

    mTextWidthCache.clear();
}

From source file:com.yek.keyboard.keyboards.views.AnyKeyboardViewBase.java

private void setSpecialKeysIconsAndLabels() {
    Keyboard.Key enterKey = findKeyByPrimaryKeyCode(KeyCodes.ENTER);
    if (enterKey != null) {
        enterKey.icon = null;// w w  w  .  j av  a2 s.  c o m
        enterKey.iconPreview = null;
        enterKey.label = null;
        ((AnyKeyboard.AnyKey) enterKey).shiftedKeyLabel = null;
        Drawable icon = getIconToDrawForKey(enterKey, false);
        if (icon != null) {
            enterKey.icon = icon;
            enterKey.iconPreview = icon;
        } else {
            CharSequence label = guessLabelForKey(enterKey.getPrimaryCode());
            enterKey.label = label;
            ((AnyKeyboard.AnyKey) enterKey).shiftedKeyLabel = label;
        }
        // making sure something is shown
        if (enterKey.icon == null && TextUtils.isEmpty(enterKey.label)) {
            Logger.i(TAG, "Wow. Unknown ACTION ID " + mKeyboardActionType + ". Will default to ENTER icon.");
            // I saw devices (Galaxy Tab 10") which say the action
            // type is 255...
            // D/ASKKbdViewBase( 3594): setKeyboardActionType
            // imeOptions:33554687 action:255
            // which means it is not a known ACTION
            Drawable enterIcon = getIconForKeyCode(KeyCodes.ENTER);
            enterIcon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_NORMAL);
            enterKey.icon = enterIcon;
            enterKey.iconPreview = enterIcon;
        }
    }
    //these are dynamic keys
    setSpecialKeyIconOrLabel(KeyCodes.MODE_ALPHABET);
    setSpecialKeyIconOrLabel(KeyCodes.MODE_SYMOBLS);
    setSpecialKeyIconOrLabel(KeyCodes.KEYBOARD_MODE_CHANGE);

    mTextWidthCache.clear();
}

From source file:com.coreform.open.android.formidablevalidation.SetErrorHandler.java

/**
  * Sets the Drawables (if any) to appear to the left of, above,
  * to the right of, and below the text.  Use null if you do not
  * want a Drawable there.  The Drawables must already have had
  * {@link Drawable#setBounds} called./*www  .ja  v  a2s.  c o  m*/
  *
  * @attr ref android.R.styleable#TextView_drawableLeft
  * @attr ref android.R.styleable#TextView_drawableTop
  * @attr ref android.R.styleable#TextView_drawableRight
  * @attr ref android.R.styleable#TextView_drawableBottom
  */
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    Drawables dr = mDrawables;

    final boolean drawables = left != null || top != null || right != null || bottom != null;

    if (!drawables) {
        // Clearing drawables...  can we free the data structure?
        if (dr != null) {
            if (dr.mDrawablePadding == 0) {
                mDrawables = null;
            } else {
                // We need to retain the last set padding, so just clear
                // out all of the fields in the existing structure.
                dr.mDrawableLeft = null;
                dr.mDrawableTop = null;
                dr.mDrawableRight = null;
                dr.mDrawableBottom = null;
                dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
                dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
                dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
                dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
            }
        }
    } else {
        if (dr == null) {
            mDrawables = dr = new Drawables();
        }

        dr.mDrawableLeft = left;
        dr.mDrawableTop = top;
        dr.mDrawableRight = right;
        dr.mDrawableBottom = bottom;

        final Rect compoundRect = dr.mCompoundRect;
        int[] state = null;

        state = mView.getDrawableState();

        if (left != null) {
            left.setState(state);
            left.copyBounds(compoundRect);
            dr.mDrawableSizeLeft = compoundRect.width();
            dr.mDrawableHeightLeft = compoundRect.height();
        } else {
            dr.mDrawableSizeLeft = dr.mDrawableHeightLeft = 0;
        }

        if (right != null) {
            right.setState(state);
            right.copyBounds(compoundRect);
            dr.mDrawableSizeRight = compoundRect.width();
            dr.mDrawableHeightRight = compoundRect.height();
        } else {
            dr.mDrawableSizeRight = dr.mDrawableHeightRight = 0;
        }

        if (top != null) {
            top.setState(state);
            top.copyBounds(compoundRect);
            dr.mDrawableSizeTop = compoundRect.height();
            dr.mDrawableWidthTop = compoundRect.width();
        } else {
            dr.mDrawableSizeTop = dr.mDrawableWidthTop = 0;
        }

        if (bottom != null) {
            bottom.setState(state);
            bottom.copyBounds(compoundRect);
            dr.mDrawableSizeBottom = compoundRect.height();
            dr.mDrawableWidthBottom = compoundRect.width();
        } else {
            dr.mDrawableSizeBottom = dr.mDrawableWidthBottom = 0;
        }
    }

    mView.invalidate();
    mView.requestLayout();
}

From source file:com.facebook.litho.LayoutState.java

void createDisplayList(LayoutOutput output) {
    ThreadUtils.assertMainThread();/*from   w w w.ja  v  a 2  s .  com*/

    final Component component = output.getComponent();
    ComponentsSystrace.beginSection("createDisplayList: " + component.getSimpleName());
    final ComponentLifecycle lifecycle = component.getLifecycle();
    final DisplayList displayList = DisplayList.createDisplayList(lifecycle.getClass().getSimpleName());

    if (displayList == null) {
        ComponentsSystrace.endSection();
        return;
    }

    final ComponentContext context = mContext;

    Drawable drawable = (Drawable) ComponentsPools.acquireMountContent(context, lifecycle.getId());
    if (drawable == null) {
        drawable = (Drawable) lifecycle.createMountContent(context);
    }

    final LayoutOutput clickableOutput = findInteractiveRoot(this, output);
    boolean isStateEnabled = false;

    if (clickableOutput != null && clickableOutput.getNodeInfo() != null) {
        final NodeInfo nodeInfo = clickableOutput.getNodeInfo();

        if (nodeInfo.hasTouchEventHandlers() || nodeInfo.getFocusState() == FOCUS_SET_TRUE) {
            isStateEnabled = true;
        }
    }

    if (isStateEnabled) {
        drawable.setState(DRAWABLE_STATE_ENABLED);
    } else {
        drawable.setState(DRAWABLE_STATE_NOT_ENABLED);
    }

    lifecycle.mount(context, drawable, component);
    lifecycle.bind(context, drawable, component);

    final Rect rect = mDisplayListCreateRect;

    output.getMountBounds(rect);
    drawable.setBounds(0, 0, rect.width(), rect.height());

    try {
        final Canvas canvas = displayList.start(rect.width(), rect.height());
        drawable.draw(canvas);

        displayList.end(canvas);
        displayList.setBounds(rect.left, rect.top, rect.right, rect.bottom);

        output.setDisplayList(displayList);
    } catch (DisplayListException e) {
        // Display list creation failed. Make sure the DisplayList for this output is set
        // to null.
        output.setDisplayList(null);
    }

    lifecycle.unbind(context, drawable, component);
    lifecycle.unmount(context, drawable, component);
    ComponentsPools.release(context, lifecycle, drawable);
    ComponentsSystrace.endSection();
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java

private Drawable getIconForKeyCode(int keyCode) {
    Drawable icon = getDrawableForKeyCode(keyCode);
    // maybe a drawable state is required
    if (icon != null) {
        switch (keyCode) {
        case KeyCodes.ENTER:
            Logger.d(TAG, "Action key action ID is %d", mKeyboardActionType);
            switch (mKeyboardActionType) {
            case EditorInfo.IME_ACTION_DONE:
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_DONE);
                break;
            case EditorInfo.IME_ACTION_GO:
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_GO);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_SEARCH);
                break;
            case EditorInfo.IME_ACTION_NONE:
            case EditorInfo.IME_ACTION_UNSPECIFIED:
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_NORMAL);
                break;
            }//  w w  w .  jav a2 s .  c  om
            break;
        case KeyCodes.SHIFT:
            if (mKeyboard.isShiftLocked())
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_MODIFIER_LOCKED);
            else if (mKeyboard.isShifted())
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_MODIFIER_PRESSED);
            else
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_MODIFIER_NORMAL);
            break;
        case KeyCodes.CTRL:
            if (mKeyboard.isControl())
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_MODIFIER_PRESSED);
            else
                icon.setState(mDrawableStatesProvider.DRAWABLE_STATE_MODIFIER_NORMAL);
            break;
        }
    }
    return icon;
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    } /* end of if */
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

private void setSpecialKeysIconsAndLabels() {
    Key enterKey = findKeyByKeyCode(KeyCodes.ENTER);
    if (enterKey != null) {
        enterKey.icon = null;//from w  ww  . j  a  v a  2s  .c  o m
        enterKey.iconPreview = null;
        enterKey.label = null;
        ((AnyKey) enterKey).shiftedKeyLabel = null;
        Drawable icon = getIconToDrawForKey(enterKey, false);
        if (icon != null) {
            enterKey.icon = icon;
            enterKey.iconPreview = icon;
        } else {
            CharSequence label = guessLabelForKey(enterKey.codes[0]);
            enterKey.label = label;
            ((AnyKey) enterKey).shiftedKeyLabel = label;
        }
        // making sure something is shown
        if (enterKey.icon == null && TextUtils.isEmpty(enterKey.label)) {
            Log.i(TAG, "Wow. Unknown ACTION ID " + mKeyboardActionType + ". Will default to ENTER icon.");
            // I saw devices (Galaxy Tab 10") which say the action
            // type is 255...
            // D/ASKKbdViewBase( 3594): setKeyboardActionType
            // imeOptions:33554687 action:255
            // which means it is not a known ACTION
            Drawable enterIcon = getIconForKeyCode(KeyCodes.ENTER);
            enterIcon.setState(mDrawableStatesProvider.DRAWABLE_STATE_ACTION_NORMAL);
            enterKey.icon = enterIcon;
            enterKey.iconPreview = enterIcon;
        }
    }
    //these are dynamic keys
    setSpecialKeyIconOrLabel(KeyCodes.MODE_ALPHABET);
    setSpecialKeyIconOrLabel(KeyCodes.MODE_SYMOBLS);
    setSpecialKeyIconOrLabel(KeyCodes.KEYBOARD_MODE_CHANGE);
}