how to make hint disappear when edittext is touched? how to make hint disappear when edittext is touched? android android

how to make hint disappear when edittext is touched?


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"


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


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