Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Hint

Tags:

android

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"/> 
like image 325
Bytecode Avatar asked Dec 07 '10 06:12

Bytecode


2 Answers

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.

like image 129
Sunit Kumar Gupta Avatar answered Sep 24 '22 03:09

Sunit Kumar Gupta


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("");   } }); 
like image 29
Aman Alam Avatar answered Sep 20 '22 03:09

Aman Alam