How to remove hint after user click / tap / focused on editText in Android


Usually we create hint in EditText in android using XML, for instance :

1
2
3
4
5
6
7
8
9
<EditText
       android:id="@+id/searchbox"
       android:layout_width="fill_parent"
       android:layout_height="40dp"
       android:hint="Iphone, Game, Camera, Books"
       android:inputType="textCapSentences"
       android:singleLine="true"
       android:textSize="11dip"
/>


But, when user tap / click or focus on EditText box, hint doesn’t dissapear. It still there until user start typing. This is a bad User Interface. We should clear hint when user click on EditText. To clear hint when user click on EditText Box, we can use setOnFocusChangeListener() and setHint().

For example :

1
2
3
4
5
6
7
8
9
final EditText search = (EditText) this.findViewById(R.id.searchbox);

// Remove Hint when people tap on search box
search.setOnFocusChangeListener(new OnFocusChangeListener() {          
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
         search.setHint("");
    }
});

Now when user tap / click on EditText, the hint will dissapear.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.