Search code examples
androidandroid-viewonclicklistenerontouchlistener

OnTouch works, but OnClickListener doesn't?


I created a widget/control that I can reuse which I created by extending RelativeLayout. Then, in one of my Activities, I created a bunch of these widgets in a loop. However, I ran into an issue when I wanted to have each widget respond to a click.

I found that setting OnTouchListener works:

 this.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View arg0, MotionEvent arg1) {
           //Triggers debug message
        }       
    });

but OnClickListener doesn't:

  this.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
           //Doesn't trigger debug message  
        }

    });

Why is this?


Solution

  • Here are a few things to check to make sure your views are clickable:

    View.setClickable()
    View.setEnabled()
    View.setFocusable()
    View.setFocusableInTouchMode()
    

    Depending on the exact behavior you hope to get, you'll need to set one or more of those to on. Since you are getting onTouch events, my guess is that you need to setClickable. But i'd have to see the view creation code to be sure.