89

I have View in which there are two text boxes, and the user can select text color from another view on the same screen (through dialog box).

So when the user changes color via dialog box, I am changing color of EditText text and its hint. But when there is some text is available in EditText after that user selects other color, then that text is coming in that color. But if I remove all that text then the color of HintText is that of the previous color.

For example, currently if I have red color in text box and the user selects green color so text is there in green color. But if I remove that text then hint text are coming in red even if I change hint color in code. This problem only comes when there is some text there. if it is blank and hint text is there then problem is not coming.

Sufian
  • 6,405
  • 16
  • 66
  • 120
kartik trivedi
  • 1,055
  • 1
  • 8
  • 11
  • 1
    Check this out http://stackoverflow.com/questions/25134463/edittext-unable-to-change-the-text-color/25134499#25134499 – Nabin Aug 05 '14 at 09:17

8 Answers8

320

Simply add this in your layout for the EditText :

android:textColorHint="#FFFFFF"

Miral Dhokiya
  • 1,720
  • 13
  • 26
Anand Chavan
  • 4,338
  • 6
  • 23
  • 27
  • 9
    How is this supposed to solve OP's question? the question is about how to change hint color DYNAMICALLY when the EditText has some text and I don't see how this will solve it... – evaristokbza Jun 05 '14 at 13:17
  • 6
    Please read the question carefully. author has not asked in appropriate manner. – Anand Chavan Jun 11 '14 at 14:48
61

Use this to change the hint color. -

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

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
            tv.setHintTextColor(getResources().getColor(R.color.white));
    }
});
Confuse
  • 5,646
  • 7
  • 36
  • 58
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
23

Default Colors:

android:textColorHint="@android:color/holo_blue_dark"

For Color code:

android:textColorHint="#33b5e5"
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
kiran kumar
  • 1,402
  • 17
  • 34
  • don't take these edits personally. Editing basically improves the readability of the answer and/or improve the completeness of it. Also, if applicable, add explanation about the code where needed, such as what is does or what problem the OP was facing, etc. :) – Sufian Feb 03 '17 at 10:04
15

Inside Layout Xml File We can Change Color of Hint.....

android:textColorHint="@android:color/*****"

you can replace * with color or color code.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Akhil s
  • 151
  • 1
  • 2
9

Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this

Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);

If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.

dishan
  • 1,346
  • 12
  • 21
6

Programmatically in Java - At least API v14+

exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));

Michael
  • 9,639
  • 3
  • 64
  • 69
  • 1
    on my samsung S5 apiV19 the same trouble, as @dishan mentioned: `setHintTextColor(color)` not working in case there is some text, EVEN if it's `""`. – Dmitry Gryazin Feb 05 '15 at 09:19
2

This is like default hint color, worked for me:

editText.setHintTextColor(Color.GRAY);

karan
  • 3,319
  • 1
  • 35
  • 44
0

You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.