max
max

Reputation: 6177

Set empty error to TextInputLayout

I have a TextInputLayout which I want to set empty error to it (I just want to make hint color and line color to be red) but when I set empty error nothing happens and my TextInputLayout don't accept it as an error and nothing changed.
any help?

and I don't want the error box to appear because I have many input text in vertical linearlayout

and I don't want the error box to be appear because I have many input text in vertical linearlayout

Upvotes: 2

Views: 992

Answers (1)

tamtom
tamtom

Reputation: 2594

I've checked the code for the setError() it has the following code

if (!mErrorEnabled) {
            if (TextUtils.isEmpty(error)) {
                // If error isn't enabled, and the error is empty, just return
                return;
            }

you can use this workaround to make the line color red

   textInputPassword.setErrorEnabled(true);
   textInputPassword.setError(" "); //white space 

for the floating hint color programmaticly you can use this code

private void setUpperHintColor(int color) {
    try {
        Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
        field.setAccessible(true);
        int[][] states = new int[][]{
                new int[]{}
        };
        int[] colors = new int[]{
                color
        };
        ColorStateList myList = new ColorStateList(states, colors);
        field.set(textInputLayout, myList);

        Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
        method.setAccessible(true);
        method.invoke(textInputLayout, true);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions