why can't I change hintTextAppearance via android:theme?

Viewed 942

Hello all I am new to android themes and styles. According to official documentations

A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a type of style that's applied to an entire app, activity, or view hierarchy—not just an individual view

But i can not change some attributes via android:theme attribute, but i can change via style

Here I have this style file:

<style name="Style.InputLayout" parent="Widget.Design.TextInputLayout">
    <item name="errorTextAppearance">@style/ErrorTextAppearance</item>
    <item name="hintTextAppearance">@style/HintTextAppearance</item>
    <item name="errorEnabled">true</item>
</style>
<style name="HintTextAppearance" parent="TextAppearance.Design.Hint">
    <!-- Inactive and Active label color-->
    <item name="android:textColor">?attr/colorShadow</item>
</style>

<style name="ErrorTextAppearance" parent="TextAppearance.Design.Error">
    <!-- Error text color-->
    <item name="android:textColor">?attr/colorError</item>
</style>

And I have a TextInputLayout as:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tfUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Style.InputLayout"
        android:hint="Username">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.EditText"/>

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

This works as i expected. But when I change style attribute to android:theme in TextInputLayout like below:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tfUsername"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Style.InputLayout"
    android:hint="Username">
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.EditText"/>

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

It does not work. Why is that?

2 Answers

It does not work. Why is that?

<com.google.android.material.textfield.TextInputLayout
    android:theme="@style/Style.InputLayout"

It doesn't work because it is wrong. You can use something like:

<com.google.android.material.textfield.TextInputLayout
        android:theme="@style/Style.MyColor"
        ...>

With:

  <style name="MyColor" parent="">
    <item name="colorPrimary">@color/.....</item>
  </style>

In this way you can modify the theme attributes for that view and any child views, which is useful for overriding theme color palettes in a specific portion of your interface.

You can find more info in the official doc.

Use like this:

Use this code on styles

<style name="MyColor" parent="">
<item name="android:colorControlActivated">@color/colorTextGray</item>
    <itemname="android:colorControlHighlight">@color/colorTextGray</item>
    <item name="android:colorControlNormal">@color/colorTextGray</item>
</style>



                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/layoutTextInput"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:theme="@style/MyColor"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/guideline131"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/guideline121"
                    app:layout_constraintTop_toBottomOf="@+id/ed_phone"
                    app:layout_constraintVertical_bias="0.0"
                    app:passwordToggleDrawable="@drawable/show_password_selector"
                    app:passwordToggleEnabled="true"
                    app:passwordToggleTint="@color/colorWhite">

                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/ed_password_mobile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="30dp"
                        android:inputType="textPassword"
                        android:textColor="@color/colorWhite"

                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/guideline131"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/guideline121"
                        app:layout_constraintTop_toBottomOf="@+id/ed_phone"
                        app:layout_constraintVertical_bias="0.0" />
                </com.google.android.material.textfield.TextInputLayout>
Related