55

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"/> 
0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This looks much appropriate option than mine!
Unfortunately, it is incorrect. <selector> elements can only be used for drawables and colors; not strings.
BUT has to be set from xml or resource id
Can you possibly expand on this, I can't find the proper documentation for the selector tag and how to do this
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(""); } }); 

2 Comments

This isn't necessary as the default behavior of the Hint on the EditText will disappear when it has focus and not empty.
This is not the best approach, you can just use a selector applied to the textColorHint. Check my answer below.
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> 

2 Comments

why is it @color/xxx and not @drawable/xxx?
@behelit textColorHint receives a color, not a drawable, so the selector must be in the color folder, not the drawable.
2
et.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { et.setHint(temp +" Characters"); } }); 

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.