Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android :: OnTouchListener && OnClickListener combination issue

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?

like image 578
Adel Iles Avatar asked Aug 25 '11 10:08

Adel Iles


People also ask

How do I turn off Touch listener on Android?

Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.

What is onTouchEvent?

onTouchEvent is a method implemented by the View, Activity and other base classes like LinearLayout, etc.. public boolean onTouchEvent(MotionEvent event) { throw new RuntimeException("Stub!" ); } you can override this method by any derived classes. whereas.

What will happen if the Ontouch () callback returns false instead of true?

If you return false than the touch event will be passed to the next View further up in the view hierarchy and you will receive no follow up calls.

What is dispatchTouchEvent?

dispatchTouchEvent is actually defined on Activity, View and ViewGroup. Think of it as a controller which decides how to route the touch events. For example, the simplest case is that of View. dispatchTouchEvent which will route the touch event to either OnTouchListener.


2 Answers

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;
    }
    });
like image 56
sven Avatar answered Sep 21 '22 20:09

sven


Adel, is the problem with the first click, or you don't get any click at all?

There is this issue if you have multiple clickable layout you don't get any click events for the first. That's because it makes it first selected and then you get the click event, try the below code.

private class CustomTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        TextView tv = (TextView) v.findViewById(R.id.single_line_text);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            tv.setTextColor(COLOR_WHEN_PRESSED);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            tv.setTextColor(COLOR_WHEN_RELEASED);
            // Action of click goes here
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            tv.setTextColor(COLOR_WHEN_RELEASED);
                            // To handle release outside the layout region
        }
        return false;
    }
}

This is working in my current implementation if you set the touch listener for your layout.

You also need to set below on your layout

android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"

Hope it helps!!!

EDIT: Additionally, there should be a flag in both DOWN and UP. Set it in DOWN and check if its set in UP. This will avoid a bug where user might tap anywhere in the screen and then hover on your textview and release it.

like image 39
Jayshil Dave Avatar answered Sep 18 '22 20:09

Jayshil Dave