Search code examples
androidandroid-event

OnTouchEvent does not work on Edittext field, is there an alternative?


I have a edittext field which prefilled with the value "comment please..." Now I want to delete this content as soon as the User touches this field. In the documentation I saw that there is the possibility to add an onTouchEvent to a edittext field. But its not working because compiler error occurs with

The method onTouchEvent(MotionEvent) in the type TextView is not applicable for the arguments (new View.OnTouchListener(){})

My code is:

        comment.onTouchEvent(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (comment.getText().equals( R.string.comment_content )){
                    comment.setText( "" );
                    return true;
                }
                return false;

            }});

thanks


Solution

  • Don't do this manually. Android already provides this functionality: "hint text".