0

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.

Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46

3 Answers3

3

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>
Deˣ
  • 4,191
  • 15
  • 24
  • When I try to set `android:hintTextAppearance`in a style, it can't be resolved at all. When I only set `hintTextAppearance` in style or directly in `TextInputLayout`, the text loses all color and becomes very small. Also, setting `textColorHint` only changes the color of the hint while the focus isn't on the `TextInputEditText`. As soon as it gets focus, the floating text, again, becomes the standard green color. – Benjamin Basmaci Jun 25 '19 at 16:13
  • I have edited the answer. I somehow messed up earlier. Let me know if this doesnt work. – Deˣ Jun 25 '19 at 16:32
0

Change you want to color in res/color - and your favorite color as define 3 color you can change effect

axar
  • 539
  • 2
  • 17
-1

You can try to set the colorAccent for the views you want, only... Something like:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/AppThemeInputLayout" />

Then, in styles.xml

<style name="AppThemeInputLayout" parent="AppTheme">
    <item name="colorAccent">#FF0000</item>
</style>

I guess this way you can change the colorAccent for specific views only (and not for whole app)

Let me know if that works for you.. Otherwise, I will delete this answer.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 1
    As I said in my last sentence, I realize that I can do that. However, thats a very unsatisfying workaround instead of a straight way to do this. What Im looking for is something like `#F00` – Benjamin Basmaci Jun 25 '19 at 16:15