Global style for TextInputLayout is not working

Viewed 482

I want to set custom style on TextInputLayout globally as:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.BudgetTracker" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        ...
        <!-- Secondary brand color. -->
        ...
        <!-- Status bar color. -->
        ...
        <!-- Customize your theme here. -->
        <item name="fontFamily">@font/regular</item>
        <!-- TextInputLayout-->
        <item name="textInputStyle">@style/Widget.MyNiceAppName.TextInputLayout.OutlinedBox</item>
    </style>

    <style name="Widget.MyNiceAppName.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="android:imeOptions">actionNext</item>
        <item name="android:maxLines">1</item>
    </style>

</resources>

and my fragment layout is

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/outlinedTextField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="32dp"
        android:hint="@string/label_email"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/acll">

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

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

but it never works. Any suggestions for mentioned issue resolution is highly appreciated.

1 Answers

You have to apply these attributes to TextInputEditText not the TextInputLayout.

In your case you can use:

<style name="Widget.MyNiceAppName.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="materialThemeOverlay">
        @style/ThemeOverlay.App.TextInputEditText.OutlinedBox
    </item>
</style>

<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="editTextStyle">@style/Widget.App.TextInputEditText.OutlinedBox</item>
</style>

<style name="Widget.App.TextInputEditText.OutlinedBox" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="android:imeOptions">actionNext</item>
    <item name="android:maxLines">1</item>
</style>

Also you can you to add the android:inputType="textEmailAddress" to the TextInputEditText:

   <com.google.android.material.textfield.TextInputEditText
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
Related