1

I am using a Textinputlayout and edittext inside it, problem is when I set the error to the textinputlayout at the time of validations , it changes the color of my hint label also to red. I want to put another color instead of that red color when error comes.Only the underline color and error text should turn to red.

enter image description here

above shown in the pic is the problem, I dont want the email label with red color.

Below is my code.

xml file

     <android.support.design.widget.TextInputLayout
            android:layout_marginTop="30dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="15dp"
 app:errorTextAppearance="@style/error_appearance"
        app:hintTextAppearance="@style/TextLabel1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et1"
            android:padding="0dp">

        <EditText
            android:id="@+id/email"
            android:hint="Email"
            android:textColorHint="#3F4B5B"
            android:maxLines="1"
            android:inputType="text"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.TextInputLayout>


#java file


#Declarations
 private TextInputLayout emailerror;
 private EditText Email;

#On create 
    Email=(EditText) findViewById(R.id.email);
     emailerror=(TextInputLayout) findViewById(R.id.et1);

#Inside validation method
 emailerror.setError("Enter a valid email");

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
         TextInputLayout text color
        <item name="colorControlNormal">#e0e0e0</item>
        <item name="colorControlActivated">#52AF44</item>
        <item name="android:textColorHint">#3F4B5B</item>
        <item name="android:windowFullscreen">true</item>
    </style>


<!--    <style name="error" parent="@android:style/TextAppearance">
        <item name="android:textColor">#ff0000</item> &lt;!&ndash;apply the color you wat here &ndash;&gt;
        <item name="android:textColorHint">#3E4A58</item>
       &lt;!&ndash; <item name="android:textColorHint">#3a52a6</item>&ndash;&gt;
       &lt;!&ndash; <item name="android:textSize">12dp</item>&ndash;&gt;
    </style>-->

    <!--<style name="Widget.Design.TextInputLayout" parent="AppTheme">
        <item name="hintTextAppearance">@style/TextLabel1</item>
        <item name="errorTextAppearance">@style/error_appearance</item>
        <item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
        <item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
    </style>-->

    <style name="error_appearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorAccent</item>
    </style>

    <style name="TextLabel1" parent="TextAppearance.Design.Hint">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="ToolbarColoredBackArrow" parent="AppTheme">
        <item name="android:textColorSecondary">@color/white</item>
    </style>

    <style name="EditTextHint" parent="Theme.AppCompat">
        <!--<item name="colorAccent">@android:color/white</item>-->
        <item name="android:textColorHint">#989898</item>
     <!--   <item name="colorControlNormal">@color/BackgroundtWhiteColor</item>
        <item name="colorControlActivated">@color/your color</item>
        <item name="colorControlHighlight">@color/BackgroundtWhiteColor</item>-->
    </style>

    <style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
        <item name="overlapAnchor">true</item>
        <item name="android:background">@drawable/spinner_background</item>
    </style>

</resources>
Yesha Shah
  • 408
  • 1
  • 5
  • 17
  • I solve it by setting the backgroundTintList (the underline, officially called Activation indicator) to red and make a TextView holding the error message. Which means I totally give up using the input Layout.error method – Mia Jul 22 '22 at 01:45

1 Answers1

0

You need to override text input layout default style, with your customstyle.

styles.xml

<resources>

 <style name="TextLabel">
    <item name="android:textColorHint">@color/colorPrimary</item> <!-- Your Hint Color -->
    <item name="android:textSize">18sp</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="colorControlNormal">@color/colorPrimary</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>

<style name="error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/colorAccent</item>
</style>

</resources>

activity_layout.xml

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:theme="@style/TextLabel"
            app:errorTextAppearance="@style/error_appearance">

            <EditText
                android:id="@+id/edt_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_phone_iphone_black_24dp"
                android:drawablePadding="8dp"
                android:drawableTint="@color/colorPrimary"
                android:hint="@string/email"
                android:inputType="number"
                android:textColor="#000" />
        </android.support.design.widget.TextInputLayout>

Focused:

Focused

Without focus:

Without focus

Error message:

enter image description here

Anil Ravsaheb Ghodake
  • 1,587
  • 2
  • 27
  • 45
  • Hello, thanks for the answer. I need to keep the hint label colour as it is for focused, not focused and at the time of error. I should never change no matter what the state is focused or not focused. – Yesha Shah Nov 17 '18 at 06:24
  • @YeshaShah So don't apply your custom hint label style, then it will take default style for textinput layout. – Anil Ravsaheb Ghodake Nov 17 '18 at 06:30
  • 1
    @YeshaShah check this https://stackoverflow.com/questions/35738544/how-to-change-the-floating-hint-color-of-textinputlayout-if-edittext-inside-is-d – AskNilesh Nov 17 '18 at 06:31
  • @AnilGhodake still both the colors are chaging. I used your code but there is no effect. – Yesha Shah Nov 17 '18 at 06:41
  • @YeshaShah, I just edited my answer. Added **error appearance** and **TextLabel1** style. Try these two styles now. Also applied these styles to textinput layout and **removed theme** applied to text input layout. – Anil Ravsaheb Ghodake Nov 17 '18 at 06:50
  • Still it is taking color of error only for errortext and labeltext both. – Yesha Shah Nov 17 '18 at 07:11
  • @YeshaShah, Can you update your question with latest implementation(including Style)? Because in my case its working properly. – Anil Ravsaheb Ghodake Nov 17 '18 at 07:18
  • I have updated my answer with style.xml @AnilGhodake – Yesha Shah Nov 17 '18 at 08:13
  • @YeshaShah, I have updated my answer now. removed hint style and added one more style and applied it as **android:theme**. This solution should be worked now. – Anil Ravsaheb Ghodake Nov 17 '18 at 08:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183813/discussion-between-anil-ghodake-and-yesha-shah). – Anil Ravsaheb Ghodake Nov 17 '18 at 08:47
  • 1
    This is not working. I also followed the above suggestions with comments too. I have spent almost 3 days for this still no luck. – Sachin Mandhare Mar 19 '20 at 12:46
  • 1
    Did anyone find a solution for this? kind of amazing that 3 years later this still an issue. – Ruan_Lopes May 03 '21 at 15:00
  • I solve it by setting the backgroundTintList (the underline, officially called Activation indicator) to red and make a TextView holding the error message. Which means I totally give up using the input Layout.error method – Mia Jul 22 '22 at 01:40