Search code examples
androidandroid-textinputlayoutandroid-textinputedittextandroid-color

How to change the floating label color of TextInputLayout and the cursor, without changing `colorAccent` in `AppTheme`


This question is related to two other questions that I'll specifically name below. The difference being, that I specifically ask for a way to change the color without the use of the base AppTheme.

In the first related question, the proposed solution includes setting

<item name="colorAccent">@color/Color Name</item> in AppTheme

However, it also says to make a theme/style specifically for the TextInputLayout, which looks like this:

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/Color Name</item> 
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/Color Name</item>
    <item name="colorControlNormal">@color/Color Name</item>
    <item name="colorControlActivated">@color/Color Name</item>
</style>

So the colorAccentis set twice. I tried this and came to the conclusion that the specific theme for the TextInputLayout is unnecessary, as I got the same result, with and without it. Only setting the colorAccent in the AppTheme is sufficient. This works but isn't necessarily what I want/need.

In the second related question, people suggest to, again, modify the AppTheme and setting colorAccent (and other attributes).

However, I don't find this solution right/satisfying. There should be a way to specifically change that one color without changing the style of the whole app, which the person posing the second question also criticizes in one of his/her comments.

So, what I am specifically looking for here, is a solution, in XML or programmatically, to specifically change the color of the floating hint/label and cursor of the TextInputLabel+TextInputEditText Combo. Basically the same behavior I'd get when setting colorAccent.

Is there a way to do this or did the SDK developers seriously make it only possible by setting colorAccent?

PS I realize that this might work with making a new Theme, inheriting from AppTheme and setting it to the activities that I want to have the specific color. However, as I said, thats not what I'm looking for.


Solution

  • Create a custom style like this:

        <style name="myInputText">
         <item name="android:textColor">@color/myColor</item>
         <item name="android:textSize">@dimen/text_size</item>
         ...
         </style>
    

    Now, you can directly use this in TextInputLayout, no need to provide any parent in style or change your app theme.

    <android.support.design.widget.TextInputLayout
        android:textColorHint="@color/myColor" 
        app:hintTextAppearance="@style/myInputText"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    

    EDIT

    This only changes the color of the floating label, not the cursor. To also change the cursor color, you need to create a drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <size android:height="10dp"
              android:width="2dp"/>
        <solid android:color="@color/myColor"/>
    </shape>
    

    and add it to the style you use for the TextInputEditText, not the TextInputLayout.

    <style name="Settings.TextInputEditText">
        ...
        <item name="android:textCursorDrawable">@drawable/cursor</item>
    </style>