0

I have this issue when using a style as seen below:

<style name="TextInputLayoutThemeWarning" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxStrokeErrorColor">@color/warning_color</item>
        <item name="errorTextColor">@color/warning_color</item>
        <item name="errorIconTint">@color/warning_color</item>
        <item name="errorIconDrawable">@drawable/ic_baseline_warning_24</item>
        <item name="textAppearanceCaption">@style/TextAppearanceTextInputLayout</item>
</style>

It applies to any TextInputLayout when they are in an activity however, when I use the same style on a TextInputLayout in a fragment the drawable and the tint are not applied. I have tried manually setting the Drawable and Tint programmatically and in XML code and it seems that these two attributes are ignored and set to the default values instead of the ones and I am choosing.

The theme for the activity is:

<style name="Theme.Default" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/CondensedFont</item>
</style>

Below is a snippet from the layout of the fragment (reduced for clarity):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/clCCS1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

    <ScrollView
        android:id="@+id/svCreateCharacter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:scrollbarSize="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clInnerContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilCharacterName"
                style="@style/TextInputLayoutThemeWarning"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="8dp"
                app:boxBackgroundColor="@color/content_background"
                app:errorEnabled="true"
                app:errorTextAppearance="@style/TextAppearanceTextInputLayout"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvCharacterTitle">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tietCharacterName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:lines="1"
                    android:maxLines="1"
                    android:textSize="12sp"
                    android:translationZ="4dp" />

            </com.google.android.material.textfield.TextInputLayout>
Michael
  • 1
  • 3

2 Answers2

0

The issue you're having may occur as the fragment may not have the correct style set as from what I have read you have to set the theme of the fragment separately which can be done using the code below in the onCreateView function in the fragment:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val contextThemeWrapper: Context = ContextThemeWrapper(activity, R.style.yourCustomTheme)
    // clone the inflater using the ContextThemeWrapper
    val localInflater = inflater.cloneInContext(contextThemeWrapper)
    return localInflater.inflate(R.layout.fragment, container, false)
}

In order for this to work however, the theme you set for the fragment must have a parent theme from the Material Design Library, in my experience and research, if the parent theme of the activity or fragment is not from Material Design Library issues can occur and widgets will not work properly, a custom theme for the fragment should be created as so in your style file:

<style name="FragmentTheme"    parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Below are some links to helpful resources where I did the research:

Set theme for a Fragment

InflateException when using TextInputLayout

  • I tried using this however, I still couldn't get the errorIconTint or errorDrawable to be changed to the desired ones. I have updated the original question any help would be more appreciated. – Michael Sep 21 '20 at 13:19
0

I figured out the reason why the Icon Tint was not applying was not due to the style not applying but rather I had added a translation z value accidentally to my TextInputLayouts. 'android:translationZ="4dp"' will cause the icon to not display properly.

Michael
  • 1
  • 3