54

I have set a hint for an EditText, currently the hint visibility is gone. When a user starts typing, I want to remove the hint text, when the cursor is visible in the EditText, not at the time when a user starts to type. How can I do that?

<EditText 
android:paddingLeft="10dp"
android:background="@drawable/edittextbg"
android:layout_marginLeft="4dp"
android:layout_marginTop="7dp"
android:gravity="left"
android:id="@+id/Photo_Comments" 
android:layout_width="150dip" 
android:maxLines="1"
android:hint="Write Caption"
android:maxLength="50"
android:singleLine="true"
android:maxWidth="100dip"
android:layout_height="wrap_content"/>
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Bytecode
  • 6,551
  • 16
  • 54
  • 101

4 Answers4

61

You can use the concept of selector. onFocus removes the hint.

android:hint="Email"

So when TextView has focus, or has user input (i.e. not empty) the hint will not display.

Ullas
  • 11,450
  • 4
  • 33
  • 50
Sunit Kumar Gupta
  • 1,203
  • 1
  • 11
  • 19
32

I don't know whether a direct way of doing this is available or not, but you surely there is a workaround via code: listen for onFocus event of EditText, and as soon it gains focus, set the hint to be nothing with something like editText.setHint(""):

This may not be exactly what you have to do, but it may be something like this-

myEditText.setOnFocusListener(new OnFocusListener(){
  public void onFocus(){
    myEditText.setHint("");
  }
});
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Aman Alam
  • 11,231
  • 7
  • 46
  • 81
  • 5
    This isn't necessary as the default behavior of the Hint on the EditText will disappear when it has focus and not empty. – w-ll Apr 22 '11 at 20:49
  • 2
    This is not the best approach, you can just use a selector applied to the textColorHint. Check my answer below. – aglour May 27 '15 at 09:09
22

To complete Sunit's answer, you can use a selector, not to the text string but to the textColorHint. You must add this attribute on your editText:

android:textColorHint="@color/text_hint_selector"

And your text_hint_selector should be:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@android:color/transparent" />
    <item android:color="@color/hint_color" />
</selector>
aglour
  • 924
  • 8
  • 15
2
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            et.setHint(temp +" Characters");
        }
    });
David K. Lee
  • 131
  • 1
  • 4
  • 4
    Hint. a good answer is more than just a dump of code. You should at least provide some bit of explanation if you intend to be A) helpful to others B) gain reputation for your answer – GhostCat Dec 17 '16 at 13:55