Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hint doesn't disappear on EditText click

I have an EditText:

<EditText 
    android:hint="Your Notes" android:id="@+id/tv_notes_data"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:gravity="right" style="@style/items_description_plain" />

I need the hint to disappear when user clicks on it, but this never happen. Please advice why.

like image 359
Eugene Avatar asked Aug 10 '11 20:08

Eugene


2 Answers

Maybe a lightly improved answer than @NitroG42's... Here's my solution:

    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

like image 91
Hugo Avatar answered Oct 23 '22 19:10

Hugo


Try to use an onClickListener:

editText.setOnClickListener(new OnClickListener {
    void onClick(View v) {
        editText.setHint("");
        // or ((EditText) v).setHint(""));
    }
});

For setting the hint again, you can use:

editText.setOnFocusChangeListener(new OnFocusChangeListener {
    void OnFocusChange(params) {
        editText.setHint("Your Notes");
    }
});

By the way, the hint disappears only when the user starts typing. In fact, while there is no characters in the EditText, the hint is shown.

like image 2
NitroG42 Avatar answered Oct 23 '22 20:10

NitroG42