Изменение цвета оттенка Android EditText программно

Я пытаюсь изменить цвет тонирования EditText View программно во время выполнения. В основном я хочу изменить то, что вы обычно применяете в качестве ?attr/colorControlNormal, например, в фоном по умолчанию.

Изменение оттенка фона неверно, просто установив new ColorsStateList одним цветом:

editText.setBackgroundTintList( ColorStateList.valueOf( color ) );

Для одного результат применяется ко всем EditText, хотя применяется список оттенков и внутренне мутирует его. Также в начале отображается альфа, как указано на фоне по умолчанию 1.

Вот результат установки цвета оттенков только на первом EditText:

outcome of setting the tint color on just the first EditText

Итак, мой вопрос: как я могу правильно применить оттенок программно к EditText?

Ответы

Ответ 1

С новой введенной установкой android.support.v4.graphics.drawable.DrawableCompat#setTint теперь возможен цвет.

Ответ 3

Попробуйте создать пользовательский EditText и добавить this.setBackgroundTintList( ColorStateList.valueOf( color ) ); в конструктор.

Ответ 4

Я написал небольшой компонент для достижения такого поведения.

Несколько важных примечаний:

  • Используется метод старой школы setColorFilter
  • Чтобы сделать оттенок, сначала переключите фокус на другой вид, затем tint EditText background drawable

Использование

ErrorLabelLayout layoutPassError = (ErrorLabelLayout) findViewById(R.id.layoutPasswordError)
layoutPassError.setError("Password_is_wrong");
// when you want to clear error e.g. in on text changed method
layoutPassError.clearError();

XML

<com.view.material.ErrorLabelLayout
    android:id="@+id/layoutPasswordError"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false">

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Enter your password"/>
</com.view.material.ErrorLabelLayout>

Источник

public class ErrorLabelLayout extends LinearLayout implements ViewGroup.OnHierarchyChangeListener {

    private static final int ERROR_LABEL_TEXT_SIZE = 12;
    private static final int ERROR_LABEL_PADDING = 4;

    private TextView mErrorLabel;
    private Drawable mDrawable;
    private int mErrorColor;

    public ErrorLabelLayout(Context context) {
        super(context);
        initView();
    }

    public ErrorLabelLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ErrorLabelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOnHierarchyChangeListener(this);
        setOrientation(VERTICAL);
        mErrorColor = Color.parseColor("#D32F2F");
        initErrorLabel();
    }

    private void initErrorLabel() {
        mErrorLabel = new TextView(getContext());
        mErrorLabel.setFocusable(true);
        mErrorLabel.setFocusableInTouchMode(true);
        mErrorLabel.setTextSize(ERROR_LABEL_TEXT_SIZE);
        mErrorLabel.setTextColor(mErrorColor);
        mErrorLabel.setPadding(dipsToPix(ERROR_LABEL_PADDING), 0, dipsToPix(ERROR_LABEL_PADDING), 0);
    }

    public void setErrorColor(int color) {
        mErrorColor = color;
        mErrorLabel.setTextColor(mErrorColor);
    }

    public void clearError() {
        mErrorLabel.setVisibility(INVISIBLE);
        mDrawable.clearColorFilter();
    }

    public void setError(String text) {
        mErrorLabel.setVisibility(VISIBLE);
        mErrorLabel.setText(text);
        // changing focus from EditText to error label, necessary for Android L only
        // EditText background Drawable is not tinted, until EditText remains focus
        mErrorLabel.requestFocus();
        // tint drawable
        mDrawable.setColorFilter(mErrorColor, PorterDuff.Mode.SRC_ATOP);
    }

    @Override
    public void onChildViewAdded(View parent, View child) {
        int childCount = getChildCount();
        if (childCount == 1) {
            mDrawable = getChildAt(0).getBackground();
            addView(mErrorLabel);
        }
    }

    @Override
    public void onChildViewRemoved(View parent, View child) {
    }

    private int dipsToPix(float dps) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dps, getResources().getDisplayMetrics());
    }
}

Протестировано на API 16/21 с библиотекой com.android.support:appcompat-v7:22.1.1.

Ответ 5

setColorFilter не работает для меня. Я использовал:

Drawable wrappedDrawable = DrawableCompat.wrap(mView.getBackground());
DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.red));
mView.setBackgroundDrawable(wrappedDrawable);

или же

DrawableCompat.setTint(mView.getBackground(), ContextCompat.getColor(this, R.color.red));

Давай попробуем.