chaithu
chaithu

Reputation: 519

TextInputLayout - Change floating label hint color in EditText in rest state

I'm using TextInputLayout with floating label hints. But In the normal state I am unable to change the hint color from white to other color. Is there a way to do this ?

<android.support.design.widget.TextInputLayout
    android:id="@+id/fullNameTextLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_weight="0.75">
    <EditText
        android:id="@+id/etFullName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:singleLine="true"
        android:hint="Full Name"
        android:textColor="@color/gray_dark"
        android:textColorHint="@color/green"
        android:textColorHighlight="@color/green" />
</android.support.design.widget.TextInputLayout>

Attaching two screen shots with the background changed.

Same view with green background

Same view with white background

Upvotes: 3

Views: 12365

Answers (5)

Dennis Allert
Dennis Allert

Reputation: 578

Got the same probleme and solved this with the setHintTextAppearance(int) method of TextInputLayout.

Example:

First find your TextInputLayout

android.support.design.widget.TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.textInputLayout);

now do the following:

textInputLayout.setHintTextAppearance(R.style.Inactive);

in your styles.xml file you create your styles with the color for the hint color. For example

<style name="Inactive" parent="AppThemeLight">
    <item name="android:textColorPrimary">@android:color/darker_gray</item>
    <item name="android:textColor">@android:color/darker_gray</item>
</style>

<style name="Active" parent="AppThemeLight">
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColor">@android:color/black</item>
</style>

now, when you change things for example onButtonClick, you simply change your style.

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
textInputLayout.setHintTextAppearance(R.style.Active);
             }
         });

Keep in mind, that you also have to handle the Colors for your EditText inside your TextInputLayout.

Upvotes: 2

user4571931
user4571931

Reputation:

Please add this in TextInputLayout,

  app:hintTextAppearance="@style/mytext 

So your layout will be like :

 <android.support.design.widget.TextInputLayout
        android:id="@+id/aeal_input_layout_zipcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/green"
        app:hintTextAppearance="@style/mytext">
    <EditText
        android:id="@+id/aeal_etZipCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Zipcode"
        android:singleLine="true"
        android:inputType="number"
        android:textColor="@color/primaryTextColor" />
</android.support.design.widget.TextInputLayout>

style.xml:

 <style name="mytext" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/green</item>
    <item name="android:textColorHint">@color/green</item>
    <item name="colorAccent">@color/green</item>
    <item name="android:textSize">14sp</item>
</style>

Edited : You need to add textColorHint in TextInputLayout, and it will work properly as you needed.

It worked for me, so might help you too.

Upvotes: 7

inkedTechie
inkedTechie

Reputation: 684

try this

<android.support.design.widget.TextInputLayout ........ android:textColorHint="requiredColor" ....> to change the floating hint color and

<EditText android:textColor="requiredColor" to change the hint color in edittxt

Upvotes: 0

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

In your Style.xml you can change the color of hint

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">#3498db</item>
</style>

For more see this

Upvotes: 0

Ahsan Kamal
Ahsan Kamal

Reputation: 1105

Use this to change the hint color. -

editText.setHintTextColor(getResources().getColor(R.color.xyz));

Solution for your problem -

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
        //do something
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        //do something
    }

    @Override
    public void afterTextChanged(Editable arg0) {
        if(arg0.toString().length() <= 0) //check if length is equal to zero
            editText.setHintTextColor(getResources().getColor(R.color.xyz));
    }
});

Upvotes: 0

Related Questions