1

I have a TextInputLayout with TextInputEditText inside. TextInputLayout realise theme to get his border color yellow when it focused and unfocused:

TextInputEditText:

style="@style/MyTextInputLayout"

MyTextInputLayout:

<style name="MyTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/edit_textbox_color</item>
</style> 

edit_textbox_color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorPrimary" android:state_focused="true"/>
    <item android:alpha="0.87" android:color="@color/colorPrimary" android:state_hovered="true"/>
    <item android:alpha="0.12" android:color="@color/colorPrimary" android:state_enabled="false"/>
    <item android:alpha="0.38" android:color="@color/colorPrimary"/>
</selector>

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout_email_wrapper"
        style="@style/MyTextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_8"
        android:layout_marginStart="@dimen/margin_medium"
        android:layout_marginTop="@dimen/margin_ultra_big"
        android:layout_marginEnd="@dimen/margin_medium"
        android:hint="Password"
        app:errorEnabled="true">

It works, but now i try to find the way to change border color if value inside field is correct.

So i write:

RxTextView.textChanges(emailNew).subscribe(
            value -> {
                isPasswordCorrect = isValidEmail(value.toString());
                if (isPasswordCorrect) {
                   //???                  
                }
            }));

..and i don't know what to do. I can't change my theme dynamically, and other solutions(like setBoxStrokeColor) only change color when field is focused.

I think i can do something with focusListener, but i think it's not good way. How can i do it correct?

TextInputEditText:

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress" />
    </com.google.android.material.textfield.TextInputLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
KirstenLy
  • 1,172
  • 3
  • 14
  • 25
  • check this https://stackoverflow.com/a/55357435/7666442 – AskNilesh Apr 01 '19 at 08:30
  • No, i want to change border color of TextInputLayout dinamicly. For current example, if isValidEmail() return true, i want one color on unfocused mode, if false - another. I can't do it with styles. – KirstenLy Apr 01 '19 at 08:50

1 Answers1

0

You can use the method textInputLayout.setBoxStrokeColor(..).

Something like:

if isValid(){
    textInputLayout.setBoxStrokeColor(...);
} else {
    textInputLayout.setBoxStrokeColor(...);
}

You can use a selector to achieve the different color when it is unfocused.
Something like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841