Gyan S Awasthi
Gyan S Awasthi

Reputation: 247

How to set color of hints in TextInputlayout?

I want to change the hint color of the Text input layout and its associated Edit Text color. I tried to use app:hintTextAppearance="@color/black"

 <android.support.design.widget.TextInputLayout
                        android:id="@+id/input_layout_edit_contact_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:textColorHighlight="@color/black"
                        app:hintTextAppearance="@color/black"
                        app:errorTextAppearance="@style/EmailErrorAppearance">

                        <EditText
                            android:id="@+id/et_input_edit_contact_name"
                            android:hint="@string/hint_name_add"
                            android:inputType="textEmailAddress"
                            android:textColor="@android:color/black"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                           />

where my style.xml is.

   <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlNormal">#FFFFFF</item>
        <item name="colorControlActivated">#FFFFFF</item>
        <item name="android:textColorHint">@color/black</item>
    </style>

 </resources>

when i change the #FFFFFF to #000000 it displays black color hint in all Textinputlayout of the application.

My need is, for one screen of the application which has white background, and on that white back ground i have to show Textinputlayout hint,text,edit text color in black font.

Upvotes: 1

Views: 975

Answers (2)

Vijay Tahelramani
Vijay Tahelramani

Reputation: 246

First of all do not specify the the property in your parent style. Because if you do so It will take the property for your whole application. Instead of doing that create a different style for TextInputLayout.

One more thing TextInputLayout used with android support library will not give you expected output. So you should migrate your Project to AndroidX and you should use Material Components library.

Add that library in your app module level gradle file.

implementation 'com.google.android.material:material:1.1.0-alpha07'

Here see the following

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxBackgroundMode">outline</item>
        <item name="boxStrokeColor">@android:color/black</item>
        <item name="android:textColorHint">@android:color/black</item>
        <item name="hintTextColor">@android:color/white</item>
    </style>

Here android:textColorHint property will handle the hint color for editTextField and hintTextColor property will handle the hint color for floating text label.

After Creating Style, Assign It to your TextInutLayout. When you don't want this style on next page don't assign the style property.

<com.google.android.material.textfield.TextInputLayout
            style="@style/TextInputLayoutStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

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


This will give you your expected output.

For more information read the documentation here on

https://material.io/develop/android/components/text-input-layout/

They have specified all the attributes that can be used for TextInputLayout.

Upvotes: 2

AGM Tazim
AGM Tazim

Reputation: 2217

Follow it -

<android.support.design.widget.TextInputLayout
  ...
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
    android:textColorHint="@color/colorPrimary">

On style.xml -

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
</style>

Upvotes: 0

Related Questions