Example usage for android.content.res ColorStateList ColorStateList

List of usage examples for android.content.res ColorStateList ColorStateList

Introduction

In this page you can find the example usage for android.content.res ColorStateList ColorStateList.

Prototype

public ColorStateList(int[][] states, @ColorInt int[] colors) 

Source Link

Document

Creates a ColorStateList that returns the specified mapping from states to colors.

Usage

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private ColorStateList createButtonColorStateList(Context context, int baseColorAttr) {
    final int[][] states = new int[4][];
    final int[] colors = new int[4];
    int i = 0;/*from  ww w  .j a v a 2s.  co m*/

    final int baseColor = ThemeUtils.getThemeAttrColor(context, baseColorAttr);
    final int colorControlHighlight = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlHighlight_ox);

    // Disabled state
    states[i] = ThemeUtils.DISABLED_STATE_SET;
    colors[i] = ThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorButtonNormal_ox);
    i++;

    states[i] = ThemeUtils.PRESSED_STATE_SET;
    colors[i] = ColorUtils.compositeColors(colorControlHighlight, baseColor);
    i++;

    states[i] = ThemeUtils.FOCUSED_STATE_SET;
    colors[i] = ColorUtils.compositeColors(colorControlHighlight, baseColor);
    i++;

    // Default enabled state
    states[i] = ThemeUtils.EMPTY_STATE_SET;
    colors[i] = baseColor;
    i++;

    return new ColorStateList(states, colors);
}

From source file:com.android.contacts.quickcontact.QuickContactActivity.java

private View inflateSuggestionListView(final Suggestion suggestion) {
    final LayoutInflater layoutInflater = LayoutInflater.from(this);
    final View suggestionView = layoutInflater.inflate(R.layout.quickcontact_suggestion_contact_item, null);

    ContactPhotoManager.DefaultImageRequest request = new ContactPhotoManager.DefaultImageRequest(
            suggestion.name, suggestion.lookupKey, ContactPhotoManager.TYPE_DEFAULT, /*
                                                                                     isCircular */ true);
    final ImageView photo = (ImageView) suggestionView.findViewById(R.id.aggregation_suggestion_photo);
    if (suggestion.photo != null) {
        ContactPhotoManager.getInstance(this).loadThumbnail(photo, suggestion.photoId, /* darkTheme */ false,
                /* isCircular */ true, request);
    } else {/*from   ww w.  ja va2 s.c om*/
        ContactPhotoManager.DEFAULT_AVATAR.applyDefaultImage(photo, -1, false, request);
    }

    final TextView name = (TextView) suggestionView.findViewById(R.id.aggregation_suggestion_name);
    name.setText(suggestion.name);

    final TextView accountNameView = (TextView) suggestionView
            .findViewById(R.id.aggregation_suggestion_account_name);
    final String accountName = suggestion.rawContacts.get(0).accountName;
    if (!TextUtils.isEmpty(accountName)) {
        accountNameView.setText(getResources().getString(R.string.contact_from_account_name, accountName));
    } else {
        accountNameView.setVisibility(View.INVISIBLE);
    }

    final CheckBox checkbox = (CheckBox) suggestionView.findViewById(R.id.suggestion_checkbox);
    final int[][] stateSet = new int[][] { new int[] { android.R.attr.state_checked },
            new int[] { -android.R.attr.state_checked } };
    final int[] colors = new int[] { mColorFilterColor, mColorFilterColor };
    if (suggestion != null && suggestion.name != null) {
        checkbox.setContentDescription(suggestion.name + " "
                + getResources().getString(R.string.contact_from_account_name, accountName));
    }
    checkbox.setButtonTintList(new ColorStateList(stateSet, colors));
    checkbox.setChecked(
            mSuggestionsShouldAutoSelected || mSelectedAggregationIds.contains(suggestion.contactId));
    if (checkbox.isChecked()) {
        mSelectedAggregationIds.add(suggestion.contactId);
    }
    checkbox.setTag(suggestion.contactId);
    checkbox.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final CheckBox checkBox = (CheckBox) v;
            final Long contactId = (Long) checkBox.getTag();
            if (mSelectedAggregationIds.contains(mContactData.getId())) {
                mSelectedAggregationIds.remove(mContactData.getId());
            }
            if (checkBox.isChecked()) {
                mSelectedAggregationIds.add(contactId);
                if (mSelectedAggregationIds.size() >= 1) {
                    enableLinkButton();
                }
            } else {
                mSelectedAggregationIds.remove(contactId);
                mSuggestionsShouldAutoSelected = false;
                if (mSelectedAggregationIds.isEmpty()) {
                    disableLinkButton();
                }
            }
        }
    });

    return suggestionView;
}

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private ColorStateList createSpinnerColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;/*from w  ww. j  av  a2s .  c o  m*/

    // Disabled state
    states[i] = ThemeUtils.DISABLED_STATE_SET;
    colors[i] = ThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorControlNormal_ox);
    i++;

    states[i] = ThemeUtils.NOT_PRESSED_OR_FOCUSED_STATE_SET;
    colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal_ox);
    i++;

    states[i] = ThemeUtils.EMPTY_STATE_SET;
    colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox);
    i++;

    return new ColorStateList(states, colors);
}

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private ColorStateList createSeekbarThumbColorStateList(Context context) {
    final int[][] states = new int[2][];
    final int[] colors = new int[2];
    int i = 0;//from   www.java2  s .  c  o m

    // Disabled state
    states[i] = ThemeUtils.DISABLED_STATE_SET;
    colors[i] = ThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorControlActivated_ox);
    i++;

    states[i] = ThemeUtils.EMPTY_STATE_SET;
    colors[i] = ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox);
    i++;

    return new ColorStateList(states, colors);
}

From source file:org.telegram.ui.ActionBar.Theme.java

public static Drawable createBarSelectorDrawable(int color, boolean masked) {
    Drawable drawable;/*from w w  w  . j av a 2s  .  co m*/
    if (Build.VERSION.SDK_INT >= 21) {
        Drawable maskDrawable = null;
        if (masked) {
            maskPaint.setColor(0xffffffff);
            maskDrawable = new Drawable() {
                @Override
                public void draw(Canvas canvas) {
                    android.graphics.Rect bounds = getBounds();
                    canvas.drawCircle(bounds.centerX(), bounds.centerY(), AndroidUtilities.dp(18), maskPaint);
                }

                @Override
                public void setAlpha(int alpha) {

                }

                @Override
                public void setColorFilter(ColorFilter colorFilter) {

                }

                @Override
                public int getOpacity() {
                    return PixelFormat.UNKNOWN;
                }
            };
        }
        ColorStateList colorStateList = new ColorStateList(new int[][] { new int[] {} }, new int[] { color });
        return new RippleDrawable(colorStateList, null, maskDrawable);
    } else {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_selected }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(color));
        stateListDrawable.addState(new int[] {}, new ColorDrawable(0x00000000));
        return stateListDrawable;
    }
}

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

public boolean setValueFromTheme(TypedArray remoteTypedArray, final int[] padding, final int localAttrId,
        final int remoteTypedArrayIndex) {
    try {/*ww w  .ja  va  2 s .c o  m*/

        if (localAttrId == android.R.attr.background) {
            Drawable keyboardBackground = remoteTypedArray.getDrawable(remoteTypedArrayIndex);
            Log.d(TAG, "AnySoftKeyboardTheme_android_background " + (keyboardBackground != null));
            super.setBackgroundDrawable(keyboardBackground);
        } else if (localAttrId == android.R.attr.paddingLeft) {
            padding[0] = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_android_paddingLeft " + padding[0]);
        } else if (localAttrId == android.R.attr.paddingTop) {
            padding[1] = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_android_paddingTop " + padding[1]);
        } else if (localAttrId == android.R.attr.paddingRight) {
            padding[2] = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_android_paddingRight " + padding[2]);
        } else if (localAttrId == android.R.attr.paddingBottom) {
            padding[3] = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_android_paddingBottom " + padding[3]);
        } else if (localAttrId == R.attr.keyBackground) {
            mKeyBackground = remoteTypedArray.getDrawable(remoteTypedArrayIndex);
            Log.d(TAG, "AnySoftKeyboardTheme_keyBackground " + (mKeyBackground != null));
        } else if (localAttrId == R.attr.keyHysteresisDistance) {
            mKeyHysteresisDistance = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_keyHysteresisDistance " + mKeyHysteresisDistance);
        } else if (localAttrId == R.attr.verticalCorrection) {
            mVerticalCorrection = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_verticalCorrection " + mVerticalCorrection);
        } else if (localAttrId == R.attr.keyPreviewBackground) {
            mPreviewKeyBackground = remoteTypedArray.getDrawable(remoteTypedArrayIndex);
            Log.d(TAG, "AnySoftKeyboardTheme_keyPreviewBackground " + (mPreviewKeyBackground != null));
        } else if (localAttrId == R.attr.keyPreviewTextColor) {
            mPreviewKeyTextColor = remoteTypedArray.getColor(remoteTypedArrayIndex, 0xFFF);
            Log.d(TAG, "AnySoftKeyboardTheme_keyPreviewTextColor " + mPreviewKeyTextColor);
        } else if (localAttrId == R.attr.keyPreviewTextSize) {
            mPreviewKeyTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_keyPreviewTextSize " + mPreviewKeyTextSize);
        } else if (localAttrId == R.attr.keyPreviewLabelTextSize) {
            mPreviewLabelTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_keyPreviewLabelTextSize " + mPreviewLabelTextSize);
        } else if (localAttrId == R.attr.keyPreviewOffset) {
            mPreviewOffset = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_keyPreviewOffset " + mPreviewOffset);
        } else if (localAttrId == R.attr.keyTextSize) {
            mKeyTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 18);
            // you might ask yourself "why did Menny sqrt root the factor?"
            // I'll tell you; the factor is mostly for the height, not the
            // font size,
            // but I also factorize the font size because I want the text to
            // be a little like
            // the key size.
            // the whole factor maybe too much, so I ease that a bit.
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                mKeyTextSize = mKeyTextSize
                        * FloatMath.sqrt(AnyApplication.getConfig().getKeysHeightFactorInLandscape());
            else
                mKeyTextSize = mKeyTextSize
                        * FloatMath.sqrt(AnyApplication.getConfig().getKeysHeightFactorInPortrait());
            Log.d(TAG, "AnySoftKeyboardTheme_keyTextSize " + mKeyTextSize);
        } else if (localAttrId == R.attr.keyTextColor) {
            mKeyTextColor = remoteTypedArray.getColorStateList(remoteTypedArrayIndex);
            if (mKeyTextColor == null) {
                Log.d(TAG, "Creating an empty ColorStateList for mKeyTextColor");
                mKeyTextColor = new ColorStateList(new int[][] { { 0 } },
                        new int[] { remoteTypedArray.getColor(remoteTypedArrayIndex, 0xFF000000) });
            }
            Log.d(TAG, "AnySoftKeyboardTheme_keyTextColor " + mKeyTextColor);
        } else if (localAttrId == R.attr.labelTextSize) {
            mLabelTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 14);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                mLabelTextSize = mLabelTextSize * AnyApplication.getConfig().getKeysHeightFactorInLandscape();
            else
                mLabelTextSize = mLabelTextSize * AnyApplication.getConfig().getKeysHeightFactorInPortrait();
            Log.d(TAG, "AnySoftKeyboardTheme_labelTextSize " + mLabelTextSize);
        } else if (localAttrId == R.attr.keyboardNameTextSize) {
            mKeyboardNameTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 10);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                mKeyboardNameTextSize = mKeyboardNameTextSize
                        * AnyApplication.getConfig().getKeysHeightFactorInLandscape();
            else
                mKeyboardNameTextSize = mKeyboardNameTextSize
                        * AnyApplication.getConfig().getKeysHeightFactorInPortrait();
            Log.d(TAG, "AnySoftKeyboardTheme_keyboardNameTextSize " + mKeyboardNameTextSize);
        } else if (localAttrId == R.attr.keyboardNameTextColor) {
            mKeyboardNameTextColor = remoteTypedArray.getColorStateList(remoteTypedArrayIndex);
            if (mKeyboardNameTextColor == null) {
                Log.d(TAG, "Creating an empty ColorStateList for mKeyboardNameTextColor");
                mKeyboardNameTextColor = new ColorStateList(new int[][] { { 0 } },
                        new int[] { remoteTypedArray.getColor(remoteTypedArrayIndex, 0xFFAAAAAA) });
            }
            Log.d(TAG, "AnySoftKeyboardTheme_keyboardNameTextColor " + mKeyboardNameTextColor);
        } else if (localAttrId == R.attr.shadowColor) {
            mShadowColor = remoteTypedArray.getColor(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_shadowColor " + mShadowColor);
        } else if (localAttrId == R.attr.shadowRadius) {
            mShadowRadius = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_shadowRadius " + mShadowRadius);
        } else if (localAttrId == R.attr.shadowOffsetX) {
            mShadowOffsetX = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_shadowOffsetX " + mShadowOffsetX);
        } else if (localAttrId == R.attr.shadowOffsetY) {
            mShadowOffsetY = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_shadowOffsetY " + mShadowOffsetY);
        } else if (localAttrId == R.attr.backgroundDimAmount) {
            mBackgroundDimAmount = remoteTypedArray.getFloat(remoteTypedArrayIndex, 0.5f);
            Log.d(TAG, "AnySoftKeyboardTheme_backgroundDimAmount " + mBackgroundDimAmount);
        } else if (localAttrId == R.attr.keyTextStyle) {
            int textStyle = remoteTypedArray.getInt(remoteTypedArrayIndex, 0);
            switch (textStyle) {
            case 0:
                mKeyTextStyle = Typeface.DEFAULT;
                break;
            case 1:
                mKeyTextStyle = Typeface.DEFAULT_BOLD;
                break;
            case 2:
                mKeyTextStyle = Typeface.defaultFromStyle(Typeface.ITALIC);
                break;
            default:
                mKeyTextStyle = Typeface.defaultFromStyle(textStyle);
                break;
            }
            Log.d(TAG, "AnySoftKeyboardTheme_keyTextStyle " + mKeyTextStyle);
        } else if (localAttrId == R.attr.symbolColorScheme) {
            mSymbolColorScheme = remoteTypedArray.getInt(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_symbolColorScheme " + mSymbolColorScheme);
        } else if (localAttrId == R.attr.keyHorizontalGap) {
            float themeHorizotalKeyGap = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            mKeyboardDimens.setHorizontalKeyGap(themeHorizotalKeyGap);
            Log.d(TAG, "AnySoftKeyboardTheme_keyHorizontalGap " + themeHorizotalKeyGap);
        } else if (localAttrId == R.attr.keyVerticalGap) {
            float themeVerticalRowGap = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            mKeyboardDimens.setVerticalRowGap(themeVerticalRowGap);
            Log.d(TAG, "AnySoftKeyboardTheme_keyVerticalGap " + themeVerticalRowGap);
        } else if (localAttrId == R.attr.keyNormalHeight) {
            float themeNormalKeyHeight = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            mKeyboardDimens.setNormalKeyHeight(themeNormalKeyHeight);
            Log.d(TAG, "AnySoftKeyboardTheme_keyNormalHeight " + themeNormalKeyHeight);
        } else if (localAttrId == R.attr.keyLargeHeight) {
            float themeLargeKeyHeight = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            mKeyboardDimens.setLargeKeyHeight(themeLargeKeyHeight);
            Log.d(TAG, "AnySoftKeyboardTheme_keyLargeHeight " + themeLargeKeyHeight);
        } else if (localAttrId == R.attr.keySmallHeight) {
            float themeSmallKeyHeight = remoteTypedArray.getDimensionPixelOffset(remoteTypedArrayIndex, 0);
            mKeyboardDimens.setSmallKeyHeight(themeSmallKeyHeight);
            Log.d(TAG, "AnySoftKeyboardTheme_keySmallHeight " + themeSmallKeyHeight);
        } else if (localAttrId == R.attr.hintTextSize) {
            mHintTextSize = remoteTypedArray.getDimensionPixelSize(remoteTypedArrayIndex, 0);
            Log.d(TAG, "AnySoftKeyboardTheme_hintTextSize " + mHintTextSize);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                mHintTextSize = mHintTextSize * AnyApplication.getConfig().getKeysHeightFactorInLandscape();
            else
                mHintTextSize = mHintTextSize * AnyApplication.getConfig().getKeysHeightFactorInPortrait();
            Log.d(TAG, "AnySoftKeyboardTheme_hintTextSize with factor " + mHintTextSize);
        } else if (localAttrId == R.attr.hintTextColor) {
            mHintTextColor = remoteTypedArray.getColorStateList(remoteTypedArrayIndex);
            if (mHintTextColor == null) {
                Log.d(TAG, "Creating an empty ColorStateList for mHintTextColor");
                mHintTextColor = new ColorStateList(new int[][] { { 0 } },
                        new int[] { remoteTypedArray.getColor(remoteTypedArrayIndex, 0xFF000000) });
            }
            Log.d(TAG, "AnySoftKeyboardTheme_hintTextColor " + mHintTextColor);
        } else if (localAttrId == R.attr.hintLabelVAlign) {
            mHintLabelVAlign = remoteTypedArray.getInt(remoteTypedArrayIndex, Gravity.BOTTOM);
            Log.d(TAG, "AnySoftKeyboardTheme_hintLabelVAlign " + mHintLabelVAlign);
        } else if (localAttrId == R.attr.hintLabelAlign) {
            mHintLabelAlign = remoteTypedArray.getInt(remoteTypedArrayIndex, Gravity.RIGHT);
            Log.d(TAG, "AnySoftKeyboardTheme_hintLabelAlign " + mHintLabelAlign);
        } else if (localAttrId == R.attr.hintOverflowLabel) {
            mHintOverflowLabel = remoteTypedArray.getString(remoteTypedArrayIndex);
            Log.d(TAG, "AnySoftKeyboardTheme_hintOverflowLabel " + mHintOverflowLabel);
        }

        return true;
    } catch (Exception e) {
        // on API changes, so the incompatible themes wont crash me..
        e.printStackTrace();
        return false;
    }
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

private static void colorTextInputLayout(TextInputLayout textInputLayout, int color) {
    try {//from w  ww  .j  a  v a2s .co  m
        ColorStateList colorStateList = new ColorStateList(new int[][] { { 0 } }, new int[] { color });
        Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(textInputLayout, colorStateList);

        Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
        fFocusedTextColor.setAccessible(true);
        fFocusedTextColor.set(textInputLayout, colorStateList);
    } catch (IllegalAccessException e) {
        L.bug(e);
    } catch (NoSuchFieldException e) {
        L.bug(e);
    }
}

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

@CallSuper
protected void onBufferDraw(Canvas canvas, final Paint paint) {
    if (mKeyboardChanged) {
        invalidateAllKeys();// www . j  av  a2s  .co  m
        mKeyboardChanged = false;
    }

    canvas.getClipBounds(mDirtyRect);

    if (mKeyboard == null)
        return;

    final boolean drawKeyboardNameText = (mKeyboardNameTextSize > 1f)
            && AnyApplication.getConfig().getShowKeyboardNameText();

    final boolean drawHintText = (mHintTextSize > 1) && AnyApplication.getConfig().getShowHintTextOnKeys();

    final boolean useCustomKeyTextColor = false;
    // TODO: final boolean useCustomKeyTextColor =
    // AnyApplication.getConfig().getUseCustomTextColorOnKeys();
    final ColorStateList keyTextColor = useCustomKeyTextColor
            ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFF6666FF })
            : mKeyTextColor;

    final boolean useCustomHintColor = drawHintText && false;
    // TODO: final boolean useCustomHintColor = drawHintText &&
    final ColorStateList hintColor = useCustomHintColor
            ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFFFF6666 })
            : mHintTextColor;

    // allow preferences to override theme settings for hint text position
    final boolean useCustomHintAlign = drawHintText && AnyApplication.getConfig().getUseCustomHintAlign();
    final int hintAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintAlign()
            : mHintLabelAlign;
    final int hintVAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintVAlign()
            : mHintLabelVAlign;

    final Drawable keyBackground = mKeyBackground;
    final Rect clipRegion = mClipRegion;
    final int kbdPaddingLeft = getPaddingLeft();
    final int kbdPaddingTop = getPaddingTop();
    final Keyboard.Key[] keys = mKeys;
    final Keyboard.Key invalidKey = mInvalidatedKey;

    boolean drawSingleKey = false;
    if (invalidKey != null && canvas.getClipBounds(clipRegion)) {
        // TODO we should use Rect.inset and Rect.contains here.
        // Is clipRegion completely contained within the invalidated key?
        if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left
                && invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top
                && invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right
                && invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) {
            drawSingleKey = true;
        }
    }

    for (Keyboard.Key keyBase : keys) {
        final AnyKeyboard.AnyKey key = (AnyKeyboard.AnyKey) keyBase;
        final boolean keyIsSpace = isSpaceKey(key);

        if (drawSingleKey && (invalidKey != key)) {
            continue;
        }
        if (!mDirtyRect.intersects(key.x + kbdPaddingLeft, key.y + kbdPaddingTop,
                key.x + key.width + kbdPaddingLeft, key.y + key.height + kbdPaddingTop)) {
            continue;
        }
        int[] drawableState = key.getCurrentDrawableState(mDrawableStatesProvider);

        if (keyIsSpace)
            paint.setColor(mKeyboardNameTextColor);
        else
            paint.setColor(keyTextColor.getColorForState(drawableState, 0xFF000000));
        keyBackground.setState(drawableState);

        // Switch the character to uppercase if shift is pressed
        CharSequence label = key.label == null ? null : adjustLabelToShiftState(key);

        final Rect bounds = keyBackground.getBounds();
        if ((key.width != bounds.right) || (key.height != bounds.bottom)) {
            keyBackground.setBounds(0, 0, key.width, key.height);
        }
        canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop);
        keyBackground.draw(canvas);

        if (TextUtils.isEmpty(label)) {
            Drawable iconToDraw = getIconToDrawForKey(key, false);
            if (iconToDraw != null/* && shouldDrawIcon */) {
                //http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent()
                //http://stackoverflow.com/a/103600/1324235
                final boolean is9Patch = iconToDraw.getCurrent() instanceof NinePatchDrawable;

                // Special handing for the upper-right number hint icons
                final int drawableWidth;
                final int drawableHeight;
                final int drawableX;
                final int drawableY;

                drawableWidth = is9Patch ? key.width : iconToDraw.getIntrinsicWidth();
                drawableHeight = is9Patch ? key.height : iconToDraw.getIntrinsicHeight();
                drawableX = (key.width + mKeyBackgroundPadding.left - mKeyBackgroundPadding.right
                        - drawableWidth) / 2;
                drawableY = (key.height + mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom
                        - drawableHeight) / 2;

                canvas.translate(drawableX, drawableY);
                iconToDraw.setBounds(0, 0, drawableWidth, drawableHeight);
                iconToDraw.draw(canvas);
                canvas.translate(-drawableX, -drawableY);
                if (keyIsSpace && drawKeyboardNameText) {
                    // now a little hack, I'll set the label now, so it get
                    // drawn.
                    label = mKeyboardName;
                }
            } else {
                // ho... no icon.
                // I'll try to guess the text
                label = guessLabelForKey(key.getPrimaryCode());
            }
        }

        if (label != null) {
            // For characters, use large font. For labels like "Done", use
            // small font.
            final FontMetrics fm;
            if (keyIsSpace) {
                paint.setTextSize(mKeyboardNameTextSize);
                paint.setTypeface(Typeface.DEFAULT_BOLD);
                if (mKeyboardNameFM == null)
                    mKeyboardNameFM = paint.getFontMetrics();
                fm = mKeyboardNameFM;
            } else if (label.length() > 1 && key.getCodesCount() < 2) {
                setPaintForLabelText(paint);
                if (mLabelFM == null)
                    mLabelFM = paint.getFontMetrics();
                fm = mLabelFM;
            } else {
                setPaintToKeyText(paint);
                if (mTextFM == null)
                    mTextFM = paint.getFontMetrics();
                fm = mTextFM;
            }

            if (isLabelOfPictographic(label)) {
                paint.setTextSize(2f * paint.getTextSize());
            }

            final float labelHeight = -fm.top;
            // Draw a drop shadow for the text
            paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor);

            final float textWidth = adjustTextSizeForLabel(paint, label, key.width);

            // the center of the drawable space, which is value used
            // previously for vertically
            // positioning the key label
            final float centerY = mKeyBackgroundPadding.top
                    + ((key.height - mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom)
                            / (keyIsSpace ? 3 : 2));// the label on the space is a bit higher

            // the X coordinate for the center of the main label text is
            // unaffected by the hints
            final float textX = mKeyBackgroundPadding.left
                    + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2;
            final float textY;
            // Some devices (mostly pre-Honeycomb, have issues with RTL text
            // drawing.
            // Of course, there is no issue with a single character :)
            // so, we'll use the RTL secured drawing (via StaticLayout) for
            // labels.
            if (label.length() > 1 && !AnyApplication.getConfig().workaround_alwaysUseDrawText()) {
                // calculate Y coordinate of top of text based on center
                // location
                textY = centerY - ((labelHeight - paint.descent()) / 2);
                canvas.translate(textX, textY);
                // RTL fix. But it costs, let do it when in need (more than
                // 1 character)
                StaticLayout labelText = new StaticLayout(label, new TextPaint(paint), (int) textWidth,
                        Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
                labelText.draw(canvas);
            } else {
                // to get Y coordinate of baseline from center of text,
                // first add half the height (to get to
                // bottom of text), then subtract the part below the
                // baseline. Note that fm.top is negative.
                textY = centerY + ((labelHeight - paint.descent()) / 2);
                canvas.translate(textX, textY);
                canvas.drawText(label, 0, label.length(), 0, 0, paint);
            }
            canvas.translate(-textX, -textY);
            // (-)

            // Turn off drop shadow
            paint.setShadowLayer(0, 0, 0, 0);
        }

        if (drawHintText) {
            if ((key.popupCharacters != null && key.popupCharacters.length() > 0) || (key.popupResId != 0)
                    || (key.longPressCode != 0)) {
                Align oldAlign = paint.getTextAlign();

                String hintText = null;

                if (key.hintLabel != null && key.hintLabel.length() > 0) {
                    hintText = key.hintLabel.toString();
                    // it is the responsibility of the keyboard layout
                    // designer to ensure that they do
                    // not put too many characters in the hint label...
                } else if (key.longPressCode != 0) {
                    if (Character.isLetterOrDigit(key.longPressCode))
                        hintText = Character.toString((char) key.longPressCode);
                } else if (key.popupCharacters != null) {
                    final String hintString = key.popupCharacters.toString();
                    final int hintLength = hintString.length();
                    if (hintLength <= 3)
                        hintText = hintString;
                }

                // if hintText is still null, it means it didn't fit one of
                // the above
                // cases, so we should provide the hint using the default
                if (hintText == null) {
                    if (mHintOverflowLabel != null)
                        hintText = mHintOverflowLabel;
                    else {
                        // theme does not provide a defaultHintLabel
                        // use  if hints are above, ... if hints are
                        // below
                        // (to avoid being too close to main label/icon)
                        if (hintVAlign == Gravity.TOP)
                            hintText = "";
                        else
                            hintText = "...";
                    }
                }

                if (mKeyboard.isShifted())
                    hintText = hintText.toUpperCase(getKeyboard().getLocale());

                // now draw hint
                paint.setTypeface(Typeface.DEFAULT);
                paint.setColor(hintColor.getColorForState(drawableState, 0xFF000000));
                paint.setTextSize(mHintTextSize);
                // get the hint text font metrics so that we know the size
                // of the hint when
                // we try to position the main label (to try to make sure
                // they don't overlap)
                if (mHintTextFM == null) {
                    mHintTextFM = paint.getFontMetrics();
                }

                final float hintX;
                final float hintY;

                // the (float) 0.5 value is added or subtracted to just give
                // a little more room
                // in case the theme designer didn't account for the hint
                // label location
                if (hintAlign == Gravity.START) {
                    // left
                    paint.setTextAlign(Align.LEFT);
                    hintX = mKeyBackgroundPadding.left + 0.5f;
                } else if (hintAlign == Gravity.CENTER) {
                    // center
                    paint.setTextAlign(Align.CENTER);
                    hintX = mKeyBackgroundPadding.left
                            + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2;
                } else {
                    // right
                    paint.setTextAlign(Align.RIGHT);
                    hintX = key.width - mKeyBackgroundPadding.right - 0.5f;
                }

                if (hintVAlign == Gravity.TOP) {
                    // above
                    hintY = mKeyBackgroundPadding.top - mHintTextFM.top + 0.5f;
                } else {
                    // below
                    hintY = key.height - mKeyBackgroundPadding.bottom - mHintTextFM.bottom - 0.5f;
                }

                canvas.drawText(hintText, hintX, hintY, paint);
                paint.setTextAlign(oldAlign);
            }
        }

        canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop);
    }
    mInvalidatedKey = null;

    mDirtyRect.setEmpty();
}

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

@CallSuper
protected void onBufferDraw(Canvas canvas, final Paint paint) {
    if (mKeyboardChanged) {
        invalidateAllKeys();/*from   w  w w. j a v a 2 s. c  o m*/
        mKeyboardChanged = false;
    }

    canvas.getClipBounds(mDirtyRect);

    if (mKeyboard == null)
        return;

    final boolean drawKeyboardNameText = (mKeyboardNameTextSize > 1f)
            && AnyApplication.getConfig().getShowKeyboardNameText();

    final boolean drawHintText = (mHintTextSize > 1) && AnyApplication.getConfig().getShowHintTextOnKeys();

    final boolean useCustomKeyTextColor = false;
    // TODO: final boolean useCustomKeyTextColor =
    // AnyApplication.getConfig().getUseCustomTextColorOnKeys();
    final ColorStateList keyTextColor = useCustomKeyTextColor
            ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFF6666FF })
            : mKeyTextColor;

    final boolean useCustomHintColor = drawHintText && false;
    // TODO: final boolean useCustomHintColor = drawHintText &&
    final ColorStateList hintColor = useCustomHintColor
            ? new ColorStateList(new int[][] { { 0 } }, new int[] { 0xFFFF6666 })
            : mHintTextColor;

    // allow preferences to override theme settings for hint text position
    final boolean useCustomHintAlign = drawHintText && AnyApplication.getConfig().getUseCustomHintAlign();
    final int hintAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintAlign()
            : mHintLabelAlign;
    final int hintVAlign = useCustomHintAlign ? AnyApplication.getConfig().getCustomHintVAlign()
            : mHintLabelVAlign;

    final Drawable keyBackground = mKeyBackground;
    final Rect clipRegion = mClipRegion;
    final int kbdPaddingLeft = getPaddingLeft();
    final int kbdPaddingTop = getPaddingTop();
    final Key[] keys = mKeys;
    final Key invalidKey = mInvalidatedKey;

    boolean drawSingleKey = false;
    if (invalidKey != null && canvas.getClipBounds(clipRegion)) {
        // TODO we should use Rect.inset and Rect.contains here.
        // Is clipRegion completely contained within the invalidated key?
        if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left
                && invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top
                && invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right
                && invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) {
            drawSingleKey = true;
        }
    }

    for (Key keyBase : keys) {
        final AnyKey key = (AnyKey) keyBase;
        final boolean keyIsSpace = isSpaceKey(key);

        if (drawSingleKey && (invalidKey != key)) {
            continue;
        }
        if (!mDirtyRect.intersects(key.x + kbdPaddingLeft, key.y + kbdPaddingTop,
                key.x + key.width + kbdPaddingLeft, key.y + key.height + kbdPaddingTop)) {
            continue;
        }
        int[] drawableState = key.getCurrentDrawableState(mDrawableStatesProvider);

        if (keyIsSpace)
            paint.setColor(mKeyboardNameTextColor);
        else
            paint.setColor(keyTextColor.getColorForState(drawableState, 0xFF000000));
        keyBackground.setState(drawableState);

        // Switch the character to uppercase if shift is pressed
        CharSequence label = key.label == null ? null : adjustLabelToShiftState(key);

        final Rect bounds = keyBackground.getBounds();
        if ((key.width != bounds.right) || (key.height != bounds.bottom)) {
            keyBackground.setBounds(0, 0, key.width, key.height);
        }
        canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop);
        keyBackground.draw(canvas);

        if (TextUtils.isEmpty(label)) {
            Drawable iconToDraw = getIconToDrawForKey(key, false);
            if (iconToDraw != null/* && shouldDrawIcon */) {
                //http://developer.android.com/reference/android/graphics/drawable/Drawable.html#getCurrent()
                //http://stackoverflow.com/a/103600/1324235
                final boolean is9Patch = iconToDraw.getCurrent() instanceof NinePatchDrawable;

                // Special handing for the upper-right number hint icons
                final int drawableWidth;
                final int drawableHeight;
                final int drawableX;
                final int drawableY;

                drawableWidth = is9Patch ? key.width : iconToDraw.getIntrinsicWidth();
                drawableHeight = is9Patch ? key.height : iconToDraw.getIntrinsicHeight();
                drawableX = (key.width + mKeyBackgroundPadding.left - mKeyBackgroundPadding.right
                        - drawableWidth) / 2;
                drawableY = (key.height + mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom
                        - drawableHeight) / 2;

                canvas.translate(drawableX, drawableY);
                iconToDraw.setBounds(0, 0, drawableWidth, drawableHeight);
                iconToDraw.draw(canvas);
                canvas.translate(-drawableX, -drawableY);
                if (keyIsSpace && drawKeyboardNameText) {
                    // now a little hack, I'll set the label now, so it get
                    // drawn.
                    label = mKeyboardName;
                }
            } else {
                // ho... no icon.
                // I'll try to guess the text
                label = guessLabelForKey(key.getPrimaryCode());
            }
        }

        if (label != null) {
            // For characters, use large font. For labels like "Done", use
            // small font.
            final FontMetrics fm;
            if (keyIsSpace) {
                paint.setTextSize(mKeyboardNameTextSize);
                paint.setTypeface(Typeface.DEFAULT_BOLD);
                if (mKeyboardNameFontMetrics == null)
                    mKeyboardNameFontMetrics = paint.getFontMetrics();
                fm = mKeyboardNameFontMetrics;
            } else if (label.length() > 1 && key.getCodesCount() < 2) {
                setPaintForLabelText(paint);
                if (mLabelFontMetrics == null)
                    mLabelFontMetrics = paint.getFontMetrics();
                fm = mLabelFontMetrics;
            } else {
                setPaintToKeyText(paint);
                if (mTextFontMetrics == null)
                    mTextFontMetrics = paint.getFontMetrics();
                fm = mTextFontMetrics;
            }

            if (isLabelOfPictographic(label)) {
                paint.setTextSize(2f * paint.getTextSize());
            }

            final float labelHeight = -fm.top;
            // Draw a drop shadow for the text
            paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor);

            final float textWidth = adjustTextSizeForLabel(paint, label, key.width);

            // the center of the drawable space, which is value used
            // previously for vertically
            // positioning the key label
            final float centerY = mKeyBackgroundPadding.top
                    + ((key.height - mKeyBackgroundPadding.top - mKeyBackgroundPadding.bottom)
                            / (keyIsSpace ? 3 : 2));// the label on the space is a bit higher

            // the X coordinate for the center of the main label text is
            // unaffected by the hints
            final float textX = mKeyBackgroundPadding.left
                    + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2;
            final float textY;
            // Some devices (mostly pre-Honeycomb, have issues with RTL text
            // drawing.
            // Of course, there is no issue with a single character :)
            // so, we'll use the RTL secured drawing (via StaticLayout) for
            // labels.
            if (label.length() > 1 && !AnyApplication.getConfig().workaround_alwaysUseDrawText()) {
                // calculate Y coordinate of top of text based on center
                // location
                textY = centerY - ((labelHeight - paint.descent()) / 2);
                canvas.translate(textX, textY);
                // RTL fix. But it costs, let do it when in need (more than
                // 1 character)
                StaticLayout labelText = new StaticLayout(label, new TextPaint(paint), (int) textWidth,
                        Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
                labelText.draw(canvas);
            } else {
                // to get Y coordinate of baseline from center of text,
                // first add half the height (to get to
                // bottom of text), then subtract the part below the
                // baseline. Note that fm.top is negative.
                textY = centerY + ((labelHeight - paint.descent()) / 2);
                canvas.translate(textX, textY);
                canvas.drawText(label, 0, label.length(), 0, 0, paint);
            }
            canvas.translate(-textX, -textY);
            // (-)

            // Turn off drop shadow
            paint.setShadowLayer(0, 0, 0, 0);
        }

        if (drawHintText) {
            if ((key.popupCharacters != null && key.popupCharacters.length() > 0) || (key.popupResId != 0)
                    || (key.longPressCode != 0)) {
                Align oldAlign = paint.getTextAlign();

                String hintText = null;

                if (key.hintLabel != null && key.hintLabel.length() > 0) {
                    hintText = key.hintLabel.toString();
                    // it is the responsibility of the keyboard layout
                    // designer to ensure that they do
                    // not put too many characters in the hint label...
                } else if (key.longPressCode != 0) {
                    if (Character.isLetterOrDigit(key.longPressCode))
                        hintText = Character.toString((char) key.longPressCode);
                } else if (key.popupCharacters != null) {
                    final String hintString = key.popupCharacters.toString();
                    final int hintLength = hintString.length();
                    if (hintLength <= 3)
                        hintText = hintString;
                }

                // if hintText is still null, it means it didn't fit one of
                // the above
                // cases, so we should provide the hint using the default
                if (hintText == null) {
                    if (mHintOverflowLabel != null)
                        hintText = mHintOverflowLabel;
                    else {
                        // theme does not provide a defaultHintLabel
                        // use  if hints are above, ... if hints are
                        // below
                        // (to avoid being too close to main label/icon)
                        if (hintVAlign == Gravity.TOP)
                            hintText = "";
                        else
                            hintText = "...";
                    }
                }

                if (mKeyboard.isShifted())
                    hintText = hintText.toUpperCase(getKeyboard().getLocale());

                // now draw hint
                paint.setTypeface(Typeface.DEFAULT);
                paint.setColor(hintColor.getColorForState(drawableState, 0xFF000000));
                paint.setTextSize(mHintTextSize);
                // get the hint text font metrics so that we know the size
                // of the hint when
                // we try to position the main label (to try to make sure
                // they don't overlap)
                if (mHintTextFontMetrics == null) {
                    mHintTextFontMetrics = paint.getFontMetrics();
                }

                final float hintX;
                final float hintY;

                // the (float) 0.5 value is added or subtracted to just give
                // a little more room
                // in case the theme designer didn't account for the hint
                // label location
                if (hintAlign == Gravity.START) {
                    // left
                    paint.setTextAlign(Align.LEFT);
                    hintX = mKeyBackgroundPadding.left + 0.5f;
                } else if (hintAlign == Gravity.CENTER) {
                    // center
                    paint.setTextAlign(Align.CENTER);
                    hintX = mKeyBackgroundPadding.left
                            + (key.width - mKeyBackgroundPadding.left - mKeyBackgroundPadding.right) / 2;
                } else {
                    // right
                    paint.setTextAlign(Align.RIGHT);
                    hintX = key.width - mKeyBackgroundPadding.right - 0.5f;
                }

                if (hintVAlign == Gravity.TOP) {
                    // above
                    hintY = mKeyBackgroundPadding.top - mHintTextFontMetrics.top + 0.5f;
                } else {
                    // below
                    hintY = key.height - mKeyBackgroundPadding.bottom - mHintTextFontMetrics.bottom - 0.5f;
                }

                canvas.drawText(hintText, hintX, hintY, paint);
                paint.setTextAlign(oldAlign);
            }
        }

        canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop);
    }
    mInvalidatedKey = null;

    mDirtyRect.setEmpty();
}

From source file:com.rks.musicx.misc.utils.Helper.java

public static ColorStateList getColorStateList(@ColorInt int color) {
    int[][] states = new int[][] { { 16842919 }, { 16842908 }, new int[0] };
    int[] colors = new int[] { getDarkerColor(color, 0.8F), getDarkerColor(color, 0.8F), color };
    return new ColorStateList(states, colors);
}