48

In my application in android are many EditText fields. And I ran into a problem with hint. It is not disappearing when EditText is focused, but it disappears when I start to write something. How can this problem be fixed? Because I need the hint to disappear when the EditText field is touched.

It is for example:

<EditText
    android:layout_width="260dp"
    android:layout_height="30dp"
    android:ems="10"
    android:inputType="text"
    android:id="@+id/driv_lic_num_reg"
    android:hint="@string/driver_license_numb"
    android:textColor="@color/black"
    android:textColorHint="@color/grey_hint"
    android:background="@drawable/input_field_background"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="2dp"
    android:textCursorDrawable="@color/black"
    android:textSize="15dp"
    android:paddingLeft="10dp"
    android:nextFocusDown="@+id/pass_reg"
    android:imeOptions="actionNext"
    android:maxLines="1"
    android:maxLength="50"
    android:focusableInTouchMode="true"/>
3

12 Answers 12

87

You can also custom your XML code by using Selector. Here is an example code.

Create a selector. In file selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@android:color/transparent" />
    <item android:color="@color/gray" />
</selector>

In your view

android:textColorHint="@drawable/selector"
7
  • 11
    This is the most elegant answer, no code involved, 100% XML. More reusable & maintainable. +1
    – voghDev
    Aug 11, 2015 at 8:12
  • 2
    Excellent answer, very concise and not code polluting. Feb 17, 2016 at 1:02
  • or just set it to transparent color "#00000000"
    – Yar
    Mar 22, 2017 at 21:28
  • 2
    The resource item should reside under res/color instead of res/drawable, shouldn't it?
    – mr5
    Apr 25, 2017 at 8:08
  • 1
    @AtikM. I had the same problem, I used app:hintTextColor="@color/selector" instead and then it worked
    – Daniel P
    Jun 11, 2021 at 22:58
59

Hint only disappears when you type in any text, not on focus. I don't think that there is any automatic way to do it, may be I am wrong. However, as a workaround I use the following code to remove hint on focus in EditText

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            myEditText.setHint("");
        else
            myEditText.setHint("Your hint");
    }
});
3
  • @Tobor that can be easily managed using some logic. I just want to give solution to hide the hint on focus. Jan 21, 2014 at 17:17
  • @user3132940 If my answer solved your problem then please accept it and mark the question solved. Jan 21, 2014 at 18:34
  • 1
    @RohanKandwal You probably meant setOnFocusChangeListener, not setOnFocusListener. Sep 29, 2015 at 1:11
10

Here's my solution, where the hint disappear when user focus editText and appears again when focus changes to other place if the edittext is still empty:

editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        editText.setHint("");
        return false;
    }
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            editText.setHint("Hint");
        }
    }
});

hope this helps someone

4

This is my solution:

final EditText editText = (EditText) findViewById(R.id.activity_edittext);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            editText .setHint("my little hint");
        } else {
            editText .setHint("");
        }
    }
});
3

The proposed selector solution by windyzboy doesn't work for EditTexts with centered text, because the cursor stays behind the transparent hint text instead of moving to the center.

A good way without polluting your code is to make a custom EditText, since you probably need the same style in the whole application. That way you can avoid adding checks for each EditText separately.

public class HideHintEditText extends EditText {

    CharSequence hint;

    public HideHintEditText(Context context) {
        super(context);
        hint = this.getHint();
    }

    public HideHintEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        hint = this.getHint();
    }

    public HideHintEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        hint = this.getHint();
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        this.setHint(focused ? "" : hint);
    }

}
1

Try

EditText editText = (EditText) findViewById(...);
editText.setHint("");

in your onTouch logic.
However your hint must be dissapearing when you clicked on edit text. So re-check your app logic.

1
  • What do you meen "re-check your app logic"? Because i'm actually don't understand why it is working like that
    – ToRaRo
    Jan 21, 2014 at 17:09
0

Here i have used the little bit of code to hide and show hint may be it'll be helpful.

txt_username.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(txt_username.getText().toString().isEmpty()){
                    if(hasFocus){
                        txt_username.setHint("");
                    }else{
                        txt_username.setHint(R.string.username);
                    }
                }
            }
        });
0
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyboard();
                editText.clearFocus();
                if (TextUtils.isEmpty(editText.getText().toString())) {
                    editText.setHint("your hint");
                }
            }
        });

        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                editText.setHint("");
                return false;
            }
        });


        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    editText.setHint("");
                else
                    editText.setHint("your hint");
            }
        });
0

My solution is like this. It shows hint when empty and not focused and hides if there is text or if it is touched

fun hideHint() {
        tilFloating.isHintAnimationEnabled = false
        etFloating.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
            if ((etFloating.text != null && etFloating.text.toString() != "") || hasFocus)
                tilFloating.hint = ""
            else
                tilFloating.hint = mHint
        }
    }
0

The way I like to do it (now in Kotlin) is to have the hint reappear if the user clicks elsewhere. Initialize the hint-string outside of your class, also importing your synthetic layout there:

import kotlinx.android.synthetic.main.activity_layout.*
private const val MY_HINT= "The Hint Shown"

Then use an OnFocusChange listener:

    myEditText.setOnFocusChangeListener() { v, event ->
        myEditText.hint = if(myEditText.hasFocus()) "" else MY_HINT
        false
    }

That way the hint doesn't get lost as the user clicks around. Or, just set it to an empty string if you don't care about that!

-1

Every Given Solution may work or not I don't know but the Following steps would definitely help.

First Create a dummy focus to restrain the EditText from gaining focus by

    <LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>

Then Normally set hint in your EditText as example:

    <EditText

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="text"
    android:ems="10"
    android:id="@+id/editText"
    android:hint="Your Hint"
    android:focusableInTouchMode="true"
   />

Finally add the following code in your activity or fragment where you intend

    editText.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            editText.setHint("");
            return false;
        }

    });
-1
android:hint="User Name"

This code line can be done that easily way!

<EditText
  android:id="@+id/edtUserName"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_weight="1"
  android:ems="10"
  android:inputType="textPersonName"
  android:hint="User Name" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.