Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style TextInputLayout programmatically

Is there a way to customize these TextInputLayout properties programmatically:

  • textColorHint
  • colorAccent
  • colorControlNormal
  • colorControlActivated
  • textSelectHandle

I know how to style them using theme attributes, but the project I'm working on loads color information dynamically, and as far as I understand there is no way of changing theme/style values at runtime.

like image 929
Ognian Gloushkov Avatar asked Oct 28 '16 15:10

Ognian Gloushkov


People also ask

How do you set TextInputLayout style programmatically?

Method 1 - Create TextInputLayout programmatically with wrapping Context with android. view. ContextThemeWrapper and use. TextInputLayout layout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.

What is a TextInputLayout?

Android TexInputLayout extends LinearLayout. The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.


1 Answers

I write textInputLayout implemention, that can set any color of underline and error and show error on top.

enter image description here

enter image description here

enter image description here

public class TextInputLayoutUseful extends TextInputLayout {
private CharSequence originalHint = "";

public TextInputLayoutUseful(Context context) {
    super(context);
    init();
}

public TextInputLayoutUseful(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public TextInputLayoutUseful(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    this.originalHint = getHint();
    setErrorEnabled(false);
}

/**
 * - put error text into top floating label
 * - colorized layout
 *
 * @param error error text
 */
@Override
public void setError(@Nullable CharSequence error) {
    if (error == null) {
        setHint(originalHint);
        setHintTextAppearance(R.style.InputLayoutNormalHint);
        setUnderlineColor(R.color.input_normal_accent);
    } else {
        setHint(error);
        setHintTextAppearance(R.style.InputLayoutErrorHint);
        setUnderlineColor(R.color.input_error_accent);
    }
}

/**
 * colorized layout specified green color
 *
 * @param acceptedHint text for floating label
 */
public void setGreenError(@NonNull CharSequence acceptedHint) {
    setHint(acceptedHint);
    setHintTextAppearance(R.style.InputLayoutAcceptedHint);
    setUnderlineColor(R.color.input_accepted_accent);
}

private void setUnderlineColor(@ColorRes int colorRes) {
    if (getEditText() != null) {
        getEditText().getBackground().mutate().setColorFilter(ContextCompat.getColor(getContext(), colorRes), PorterDuff.Mode.SRC_ATOP);
    }
}
}

style

<style name="InputLayoutErrorHint" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/input_error_accent</item>
</style>

<style name="InputLayoutNormalHint" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/input_normal_accent</item>
</style>

<style name="InputLayoutAcceptedHint" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/input_accepted_accent</item>
</style>
like image 77
Gogi Bobina Avatar answered Oct 05 '22 17:10

Gogi Bobina