androidandroid-themematerial-components-androidandroid-textinputlayouttextinputlayout

Subclass TextInputLayout uses broken theme


I need to subclass TextInputLayout to provide a custom behaviour. However, even with an empty body, the layout picks up a different theme from somewhere. I would not be too worried if I am able to override the theme but this theme is just straight up broken, e.g. error drawable will not show.

Vanilla layouts:

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/login_username_input_layoutz"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_username_hint">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/login_username_input_textz"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.textfield.TextInputLayout>

enter image description here


Subclassed layout:

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.textfield.TextInputLayout

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {}

            <somepackage.CustomTextInputLayout
                android:id="@+id/login_username_input_layoutz"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_username_hint">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/login_username_input_textz"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </somepackage.CustomTextInputLayout >

enter image description here


Material design import: com.google.android.material:material:1.2.0

Parent app theme: Theme.MaterialComponents.Light.NoActionBar


Solution

  • In the constructor use defStyleAttr: Int = R.attr.textInputStyle instead of defStyleAttr: Int = 0