Search code examples
androidandroid-themeandroid-textinputlayout

How to change default color of Android Floating Label for TextInputLayout?


I am able to change the color of a floating label for a TextInputLayout that is wrapping an EditText. When it floats up during the animation phase using the below code I am able to select the color black (default is white) using "android:textColor". I am trying to select a different color when the EditText becomes unfocused--meaning when the floated label becomes permanent above the EditText. My minimum API is 14. Please advise.

partial themes.xml file:

<?xml version="1.0" encoding="utf-8"?>

<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar" >
...
</style>

<style name="FloatingLabel" parent="@android:style/TextAppearance" >
    <item name="android:textColor">#000000</item>
</style>

partial layout.xml file:

...
<android.support.design.widget.TextInputLayout
    android:id="@+id/ToDo_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@style/FloatingLabel"
    >
...

Solution

  • Add this in styles:

    <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/red</item>
        <item name="android:textSize">14sp</item>
    </style>
    

    Add this in your layout:

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/gray"
       **app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"**>
    
        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint" />
    </android.support.design.widget.TextInputLayout>