setHintTextColor() in EditText

AndroidAndroid EdittextAndroid Text-Color

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Simply add this in your layout for the EditText :

>android:textColorHint="#FFFFFF"

Solution 2 - Android

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));
	}
});

Solution 3 - Android

Default Colors:

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

For Color code:

android:textColorHint="#33b5e5"

Solution 4 - Android

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

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

you can replace ***** with color or color code.

Solution 5 - Android

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.

Solution 6 - Android

Programmatically in Java - At least API v14+

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

Solution 7 - Android

This is like default hint color, worked for me:

editText.setHintTextColor(Color.GRAY);

Solution 8 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkartik trivediView Question on Stackoverflow
Solution 1 - AndroidAnand ChavanView Answer on Stackoverflow
Solution 2 - AndroidSunil Kumar SahooView Answer on Stackoverflow
Solution 3 - Androidkiran kumarView Answer on Stackoverflow
Solution 4 - AndroidAkhil sView Answer on Stackoverflow
Solution 5 - AndroiddishanView Answer on Stackoverflow
Solution 6 - AndroidMichaelView Answer on Stackoverflow
Solution 7 - AndroidkaranView Answer on Stackoverflow
Solution 8 - AndroidranbimsView Answer on Stackoverflow