Example usage for android.content.res ColorStateList valueOf

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

Introduction

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

Prototype

@NonNull
public static ColorStateList valueOf(@ColorInt int color) 

Source Link

Usage

From source file:com.example.michaelg.myapplication.Item.discreteseekbar.DiscreteSeekBar.java

/**
 * Sets the color of the seekbar scrubber
 *
 * @param color The color the track will be changed to
 *//*w w  w  . ja  va  2 s  . c  om*/
public void setTrackColor(int color) {
    mTrack.setColorStateList(ColorStateList.valueOf(color));
}

From source file:xyz.berial.textinputlayout.TextInputLayout.java

/**
 * Sets an error message that will be displayed below our {@link EditText}. If the
 * {@code error} is {@code null}, the error message will be cleared.
 * <p/>/*from   w  ww  . jav  a 2  s.c  o  m*/
 * If the error functionality has not been enabled via {@link #setErrorEnabled(boolean)}, then
 * it will be automatically enabled if {@code error} is not empty.
 *
 * @param error Error message to display, or null to clear
 * @see #getError()
 */
public void setError(@Nullable CharSequence error) {
    if (!mErrorEnabled) {
        if (TextUtils.isEmpty(error)) {
            // If error isn't enabled, and the error is empty, just return
            return;
        }
        // Else, we'll assume that they want to enable the error functionality
        setErrorEnabled(true);
    }

    if (!TextUtils.isEmpty(error)) {
        ViewCompat.setAlpha(mErrorView, 0f);
        mErrorView.setText(error);
        ViewCompat.animate(mErrorView).alpha(1f).setDuration(ANIMATION_DURATION)
                .setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
                .setListener(new ViewPropertyAnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(View view) {
                        view.setVisibility(VISIBLE);
                    }
                }).start();

        // Set the EditText's background tint to the error color
        ViewCompat.setBackgroundTintList(mEditText, ColorStateList.valueOf(mErrorView.getCurrentTextColor()));
    } else {
        if (mErrorView.getVisibility() == VISIBLE) {
            ViewCompat.animate(mErrorView).alpha(0f).setDuration(ANIMATION_DURATION)
                    .setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
                    .setListener(new ViewPropertyAnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(View view) {
                            view.setVisibility(INVISIBLE);
                        }
                    }).start();

            /*custom*/
            if (mEditText.length() > mCounterMaxLength) {
                return;
            }
            /*custom*/
            // Restore the 'original' tint, using colorControlNormal and colorControlActivated
            final TintManager tintManager = TintManager.get(getContext());
            ViewCompat.setBackgroundTintList(mEditText,
                    tintManager.getTintList(R.drawable.abc_edit_text_material));
        }
    }

    sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
}

From source file:cn.edu.qzu.face.PagerSlidingTabStrip.java

public void setTextColor(int textColor) {
    //      this.tabTextColor = textColor;
    this.tabTextColorStateList = ColorStateList.valueOf(textColor);
    updateTabStyles();//from  w  w  w.j  a va 2  s. co m
}

From source file:com.github.shareme.gwsmaterialuikit.library.material.widget.EditText.java

@Override
protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super.applyStyle(context, attrs, defStyleAttr, defStyleRes);

    CharSequence text = mInputView == null ? null : mInputView.getText();
    removeAllViews();//from w  ww. j  a  v  a 2 s.c  o  m

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditText, defStyleAttr, defStyleRes);

    int labelPadding = -1;
    int labelTextSize = -1;
    ColorStateList labelTextColor = null;
    int supportPadding = -1;
    int supportTextSize = -1;
    ColorStateList supportColors = null;
    ColorStateList supportErrorColors = null;
    String supportHelper = null;
    String supportError = null;
    ColorStateList dividerColors = null;
    ColorStateList dividerErrorColors = null;
    int dividerHeight = -1;
    int dividerPadding = -1;
    int dividerAnimDuration = -1;

    mAutoCompleteMode = a.getInteger(R.styleable.EditText_et_autoCompleteMode, mAutoCompleteMode);
    if (needCreateInputView(mAutoCompleteMode)) {
        switch (mAutoCompleteMode) {
        case AUTOCOMPLETE_MODE_SINGLE:
            mInputView = new InternalAutoCompleteTextView(context, attrs, defStyleAttr);
            break;
        case AUTOCOMPLETE_MODE_MULTI:
            mInputView = new InternalMultiAutoCompleteTextView(context, attrs, defStyleAttr);
            break;
        default:
            mInputView = new InternalEditText(context, attrs, defStyleAttr);
            break;
        }
        ViewUtil.applyFont(mInputView, attrs, defStyleAttr, defStyleRes);
        if (text != null)
            mInputView.setText(text);

        mInputView.addTextChangedListener(new InputTextWatcher());

        if (mDivider != null) {
            mDivider.setAnimEnable(false);
            ViewUtil.setBackground(mInputView, mDivider);
            mDivider.setAnimEnable(true);
        }
    } else
        ViewUtil.applyStyle(mInputView, attrs, defStyleAttr, defStyleRes);
    mInputView.setVisibility(View.VISIBLE);
    mInputView.setFocusableInTouchMode(true);

    for (int i = 0, count = a.getIndexCount(); i < count; i++) {
        int attr = a.getIndex(i);

        if (attr == R.styleable.EditText_et_labelEnable)
            mLabelEnable = a.getBoolean(attr, false);
        else if (attr == R.styleable.EditText_et_labelPadding)
            labelPadding = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_labelTextSize)
            labelTextSize = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_labelTextColor)
            labelTextColor = a.getColorStateList(attr);
        else if (attr == R.styleable.EditText_et_labelTextAppearance)
            getLabelView().setTextAppearance(context, a.getResourceId(attr, 0));
        else if (attr == R.styleable.EditText_et_labelEllipsize) {
            int labelEllipsize = a.getInteger(attr, 0);
            switch (labelEllipsize) {
            case 1:
                getLabelView().setEllipsize(TruncateAt.START);
                break;
            case 2:
                getLabelView().setEllipsize(TruncateAt.MIDDLE);
                break;
            case 3:
                getLabelView().setEllipsize(TruncateAt.END);
                break;
            case 4:
                getLabelView().setEllipsize(TruncateAt.MARQUEE);
                break;
            default:
                getLabelView().setEllipsize(TruncateAt.END);
                break;
            }
        } else if (attr == R.styleable.EditText_et_labelInAnim)
            mLabelInAnimId = a.getResourceId(attr, 0);
        else if (attr == R.styleable.EditText_et_labelOutAnim)
            mLabelOutAnimId = a.getResourceId(attr, 0);
        else if (attr == R.styleable.EditText_et_supportMode)
            mSupportMode = a.getInteger(attr, 0);
        else if (attr == R.styleable.EditText_et_supportPadding)
            supportPadding = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_supportTextSize)
            supportTextSize = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_supportTextColor)
            supportColors = a.getColorStateList(attr);
        else if (attr == R.styleable.EditText_et_supportTextErrorColor)
            supportErrorColors = a.getColorStateList(attr);
        else if (attr == R.styleable.EditText_et_supportTextAppearance)
            getSupportView().setTextAppearance(context, a.getResourceId(attr, 0));
        else if (attr == R.styleable.EditText_et_supportEllipsize) {
            int supportEllipsize = a.getInteger(attr, 0);
            switch (supportEllipsize) {
            case 1:
                getSupportView().setEllipsize(TruncateAt.START);
                break;
            case 2:
                getSupportView().setEllipsize(TruncateAt.MIDDLE);
                break;
            case 3:
                getSupportView().setEllipsize(TruncateAt.END);
                break;
            case 4:
                getSupportView().setEllipsize(TruncateAt.MARQUEE);
                break;
            default:
                getSupportView().setEllipsize(TruncateAt.END);
                break;
            }
        } else if (attr == R.styleable.EditText_et_supportMaxLines)
            getSupportView().setMaxLines(a.getInteger(attr, 0));
        else if (attr == R.styleable.EditText_et_supportLines)
            getSupportView().setLines(a.getInteger(attr, 0));
        else if (attr == R.styleable.EditText_et_supportSingleLine)
            getSupportView().setSingleLine(a.getBoolean(attr, false));
        else if (attr == R.styleable.EditText_et_supportMaxChars)
            mSupportMaxChars = a.getInteger(attr, 0);
        else if (attr == R.styleable.EditText_et_helper)
            supportHelper = a.getString(attr);
        else if (attr == R.styleable.EditText_et_error)
            supportError = a.getString(attr);
        else if (attr == R.styleable.EditText_et_inputId)
            mInputView.setId(a.getResourceId(attr, 0));
        else if (attr == R.styleable.EditText_et_dividerColor)
            dividerColors = a.getColorStateList(attr);
        else if (attr == R.styleable.EditText_et_dividerErrorColor)
            dividerErrorColors = a.getColorStateList(attr);
        else if (attr == R.styleable.EditText_et_dividerHeight)
            dividerHeight = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_dividerPadding)
            dividerPadding = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.EditText_et_dividerAnimDuration)
            dividerAnimDuration = a.getInteger(attr, 0);
        else if (attr == R.styleable.EditText_et_dividerCompoundPadding)
            mDividerCompoundPadding = a.getBoolean(attr, true);
    }

    a.recycle();

    if (mInputView.getId() == 0)
        mInputView.setId(ViewUtil.generateViewId());

    if (mDivider == null) {
        mDividerColors = dividerColors;
        mDividerErrorColors = dividerErrorColors;

        if (mDividerColors == null) {
            int[][] states = new int[][] { new int[] { -android.R.attr.state_focused },
                    new int[] { android.R.attr.state_focused, android.R.attr.state_enabled }, };
            int[] colors = new int[] { ThemeUtil.colorControlNormal(context, 0xFF000000),
                    ThemeUtil.colorControlActivated(context, 0xFF000000), };

            mDividerColors = new ColorStateList(states, colors);
        }

        if (mDividerErrorColors == null)
            mDividerErrorColors = ColorStateList.valueOf(ThemeUtil.colorAccent(context, 0xFFFF0000));

        if (dividerHeight < 0)
            dividerHeight = 0;

        if (dividerPadding < 0)
            dividerPadding = 0;

        if (dividerAnimDuration < 0)
            dividerAnimDuration = context.getResources().getInteger(android.R.integer.config_shortAnimTime);

        mDividerPadding = dividerPadding;
        mInputView.setPadding(0, 0, 0, mDividerPadding + dividerHeight);

        mDivider = new DividerDrawable(dividerHeight,
                mDividerCompoundPadding ? mInputView.getTotalPaddingLeft() : 0,
                mDividerCompoundPadding ? mInputView.getTotalPaddingRight() : 0, mDividerColors,
                dividerAnimDuration);
        mDivider.setInEditMode(isInEditMode());
        mDivider.setAnimEnable(false);
        ViewUtil.setBackground(mInputView, mDivider);
        mDivider.setAnimEnable(true);
    } else {
        if (dividerHeight >= 0 || dividerPadding >= 0) {
            if (dividerHeight < 0)
                dividerHeight = mDivider.getDividerHeight();

            if (dividerPadding >= 0)
                mDividerPadding = dividerPadding;

            mInputView.setPadding(0, 0, 0, mDividerPadding + dividerHeight);
            mDivider.setDividerHeight(dividerHeight);
            mDivider.setPadding(mDividerCompoundPadding ? mInputView.getTotalPaddingLeft() : 0,
                    mDividerCompoundPadding ? mInputView.getTotalPaddingRight() : 0);
        }

        if (dividerColors != null)
            mDividerColors = dividerColors;

        if (dividerErrorColors != null)
            mDividerErrorColors = dividerErrorColors;

        mDivider.setColor(getError() == null ? mDividerColors : mDividerErrorColors);

        if (dividerAnimDuration >= 0)
            mDivider.setAnimationDuration(dividerAnimDuration);
    }

    if (labelPadding >= 0)
        getLabelView().setPadding(mDivider.getPaddingLeft(), 0, mDivider.getPaddingRight(), labelPadding);

    if (labelTextSize >= 0)
        getLabelView().setTextSize(TypedValue.COMPLEX_UNIT_PX, labelTextSize);

    if (labelTextColor != null)
        getLabelView().setTextColor(labelTextColor);

    if (mLabelEnable) {
        mLabelVisible = true;
        getLabelView().setText(mInputView.getHint());
        addView(getLabelView(), 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        setLabelVisible(!TextUtils.isEmpty(mInputView.getText().toString()), false);
    }

    if (supportTextSize >= 0)
        getSupportView().setTextSize(TypedValue.COMPLEX_UNIT_PX, supportTextSize);

    if (supportColors != null)
        mSupportColors = supportColors;
    else if (mSupportColors == null)
        mSupportColors = context.getResources().getColorStateList(R.color.abc_secondary_text_material_light);

    if (supportErrorColors != null)
        mSupportErrorColors = supportErrorColors;
    else if (mSupportErrorColors == null)
        mSupportErrorColors = ColorStateList.valueOf(ThemeUtil.colorAccent(context, 0xFFFF0000));

    if (supportPadding >= 0)
        getSupportView().setPadding(mDivider.getPaddingLeft(), supportPadding, mDivider.getPaddingRight(), 0);

    if (supportHelper == null && supportError == null)
        getSupportView().setTextColor(getError() == null ? mSupportColors : mSupportErrorColors);
    else if (supportHelper != null)
        setHelper(supportHelper);
    else
        setError(supportError);

    if (mSupportMode != SUPPORT_MODE_NONE) {
        switch (mSupportMode) {
        case SUPPORT_MODE_CHAR_COUNTER:
            getSupportView().setGravity(Gravity.END);
            updateCharCounter(mInputView.getText().length());
            break;
        case SUPPORT_MODE_HELPER:
        case SUPPORT_MODE_HELPER_WITH_ERROR:
            getSupportView().setGravity(GravityCompat.START);
            break;
        }
        addView(getSupportView(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    addView(mInputView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    requestLayout();
}

From source file:org.getlantern.firetweet.util.ThemeUtils.java

private static void applyColorTintForView(View view, int tintColor) {
    if (view instanceof IThemedView) {
        final ColorStateList tintList = ColorStateList.valueOf(tintColor);
        ((IThemedView) view).setThemeTintColor(tintList);
    } else if (view instanceof ProgressBar) {
        final ColorStateList tintList = ColorStateList.valueOf(tintColor);
        final ProgressBar progressBar = (ProgressBar) view;
        ViewAccessor.setProgressTintList(progressBar, tintList);
        ViewAccessor.setProgressBackgroundTintList(progressBar, tintList);
        ViewAccessor.setIndeterminateTintList(progressBar, tintList);
    } else if (view instanceof CompoundButton) {
        final ColorStateList tintList = ColorStateList.valueOf(tintColor);
        final CompoundButton compoundButton = (CompoundButton) view;
        ViewAccessor.setButtonTintList(compoundButton, tintList);
    }/*from  w ww . j  a  va 2  s  .  c  om*/
    // TODO support TintableBackgroundView
}

From source file:com.philliphsu.bottomsheetpickers.time.numberpad.NumberPadTimePicker.java

private void updateFabState() {
    final boolean lastEnabled = mFab.isEnabled();
    mFab.setEnabled(checkTimeValid());/*w  ww  .  j a va  2  s  . co m*/
    // If the fab was last enabled and we rotate, this check will prevent us from
    // restoring the color; it will instead show up opaque white with an eclipse.
    // Why isn't the FAB initialized to enabled == false when it is recreated?
    // The FAB class probably saves its own state.
    //        if (lastEnabled == mFab.isEnabled())
    //            return;

    // Workaround for mFab.setBackgroundTintList() because I don't know how to reference the
    // correct accent color in XML. Also because I don't want to programmatically create a
    // ColorStateList.
    int color;
    if (mFab.isEnabled()) {
        color = mAccentColor;
        // If FAB was last enabled, then don't run the anim again.
        if (mElevationAnimator != null && !lastEnabled) {
            mElevationAnimator.start();
        }
    } else {
        color = mThemeDark ? mFabDisabledColorDark : mFabDisabledColorLight;
        if (lastEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (mElevationAnimator != null && mElevationAnimator.isRunning()) {
                // Otherwise, eclipse will show.
                mElevationAnimator.end();
            }
            // No animation, otherwise we'll see eclipsing.
            mFab.setElevation(0);
        }
    }
    // TODO: How can we animate the background color? There is a ObjectAnimator.ofArgb()
    // method, but that uses color ints as values. What we'd really need is something like
    // ColorStateLists as values. There is an ObjectAnimator.ofObject(), but I don't know
    // how that works. There is also a ValueAnimator.ofInt(), which doesn't need a
    // target object.
    mFab.setBackgroundTintList(ColorStateList.valueOf(color));
}

From source file:com.rks.musicx.ui.fragments.PlayingViews.Playing2Fragment.java

private void colorMode(int color) {
    if (getActivity() == null || getActivity().getWindow() == null) {
        return;/*  w w w .  j  a  va2 s.  c  o  m*/
    }
    if (Extras.getInstance().getDarkTheme() || Extras.getInstance().getBlackTheme()) {
        getActivity().getWindow().setNavigationBarColor(color);
        mPlayLayout.setBigDiffuserColor(Helper.getColorWithAplha(color, 0.3f));
        mPlayLayout.setMediumDiffuserColor(Helper.getColorWithAplha(color, 0.4f));
        mPlayLayout.getPlayButton().setBackgroundTintList(ColorStateList.valueOf(accentColor));
        mPlayLayout.setProgressBallColor(color);
        mPlayLayout.setProgressCompleteColor(color);
        getActivity().getWindow().setStatusBarColor(color);
    } else {
        getActivity().getWindow().setNavigationBarColor(color);
        mPlayLayout.setBigDiffuserColor(Helper.getColorWithAplha(color, 0.3f));
        mPlayLayout.setMediumDiffuserColor(Helper.getColorWithAplha(color, 0.4f));
        mPlayLayout.getPlayButton().setBackgroundTintList(ColorStateList.valueOf(accentColor));
        mPlayLayout.setProgressBallColor(color);
        mPlayLayout.setProgressCompleteColor(color);
        getActivity().getWindow().setStatusBarColor(color);
    }

}

From source file:com.afwsamples.testdpc.SetupManagementFragment.java

@Override
public void onColorSelected(int colorValue, String id) {
    mCurrentColor = colorValue;/*from   w  ww.j  a v  a2  s  . co m*/
    mColorValue.setText(String.format(ColorPicker.COLOR_STRING_FORMATTER, colorValue));
    mColorPreviewView.setImageTintList(ColorStateList.valueOf(colorValue));
}

From source file:uk.ac.horizon.artcodes.scanner.ScannerActivity.java

private void createSettingsUI(List<DetectorSetting> settings) {
    settingIcons.removeAllViews();//from  ww w .ja  va  2  s  .  c  o m
    if (settings != null && !settings.isEmpty()) {
        final int padding = getResources().getDimensionPixelSize(R.dimen.setting_padding);
        List<DetectorSetting> settingList = settings;
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            settingList = Lists.reverse(settings);
        }
        for (final DetectorSetting setting : settingList) {
            final ImageView button = new ImageView(this);
            button.setContentDescription(getString(setting.getText()));
            final int[] attrs = new int[] { android.R.attr.selectableItemBackground };
            final TypedArray ta = obtainStyledAttributes(attrs);
            final Drawable drawableFromTheme = ta.getDrawable(0);
            ta.recycle();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                button.setImageResource(setting.getIcon());
                button.setImageTintList(ColorStateList.valueOf(Color.WHITE));
                button.setBackground(drawableFromTheme);
            } else {
                button.setImageDrawable(getTintedDrawable(setting.getIcon(), Color.WHITE));
                //noinspection deprecation
                button.setBackgroundDrawable(drawableFromTheme);
            }

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setting.nextValue();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        button.setImageResource(setting.getIcon());
                    } else {
                        button.setImageDrawable(getTintedDrawable(setting.getIcon(), Color.WHITE));
                    }
                    button.setContentDescription(getString(setting.getText()));
                    textAnimator.setText(setting.getText());
                }
            });
            final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
            params.weight = 1;
            button.setLayoutParams(params);
            settingIcons.addView(button);
            button.setPadding(padding, padding, padding, padding);
        }

    }

    menuAnimator.setViewVisible(settingIcons.getChildCount() > 0);
}

From source file:com.rks.musicx.ui.fragments.PlayingViews.Playing3Fragment.java

private void colorMode(int color) {
    if (getActivity() == null || getActivity().getWindow() == null) {
        return;//  w ww.  ja va  2 s.c  o  m
    }
    if (Extras.getInstance().getDarkTheme() || Extras.getInstance().getBlackTheme()) {
        getActivity().getWindow().setNavigationBarColor(color);
        getActivity().getWindow().setStatusBarColor(color);
        seekbar.setBackgroundTintList(ColorStateList.valueOf(color));
    } else {
        getActivity().getWindow().setNavigationBarColor(color);
        seekbar.setBackgroundTintList(ColorStateList.valueOf(color));
        getActivity().getWindow().setStatusBarColor(color);
    }

}