Android :: OnTouchListener && OnClickListener combination issue

Adel Iles picture Adel Iles · Aug 25, 2011 · Viewed 17.7k times · Source

Problem description:

I have a TextView on a RelativeLayout and I want to color it red when the user touches it, and go on another page when he clicks on it. So I tried to set an OnClickListener to do the click, and an OnTouchListener to implement the touch function (MotionEvent.ACTION_DOWN) but this combination doesn't work, because OnTouchListener makes OnClickListener non-functional (don't know why).

On forums people say that we can implement the OnClick by the OnTouch MotionEvent.ACTION_UP, but this one can be triggered out of my TextView layout (the TextView gonna be clicked if you press it and drag your finger out of him to release) and this is not the desired behavior because I want:
click = press + release on the TextView.

Can someone give me a solution for this please?

Answer

sven picture sven · Mar 10, 2012

you may call View.performClick() when action_up. Hope it helps.

your_txtView.setOnClickListener(new TextView.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
            @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {

        } else if (MotionEvent.ACTION_UP == event.getAction()) {
            v.performClick();
        }

        return true;
    }
    });