Android EditText Hint

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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>

Solution 4 - Android

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            
            et.setHint(temp +" Characters");
        }
    });

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
QuestionBytecodeView Question on Stackoverflow
Solution 1 - AndroidSunit Kumar GuptaView Answer on Stackoverflow
Solution 2 - AndroidAman AlamView Answer on Stackoverflow
Solution 3 - AndroidaglourView Answer on Stackoverflow
Solution 4 - AndroidDavid K. LeeView Answer on Stackoverflow