Amit Tiwari
Amit Tiwari

Reputation: 3692

OnClickListener getting called twice in Android

I have the following use case: when clicking on a View, it should open an Activity and when long clicked, it should open a different Activity after a time which is minimum of, 300ms or user's ACTION_UP event. I have written the following code for this:

holder.fiveAction.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //open activity A after launching an animation of 300ms
                isLongPressed = true;
                return true;
            }
        });
        holder.fiveAction.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                // We're only interested in when the button is released.
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    if (isLongPressed) {
                        //open activity A even if animation hasn't ended
                        // Do something when the button is released.
                        isLongPressed = false;
                    }
                }
                return false;
            }
        });

        holder.fiveAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtil.i(TAG, "hi five onClickListener called");
                //open some activity B or do some work
        });

The issue with this setup is that the OnClickListener gets called twice which I don't want. I want to know why is this happening and how can I fix this without compromising my use case.

Upvotes: 4

Views: 5882

Answers (2)

Nigel N.
Nigel N.

Reputation: 337

I had this problem because of the buggy Android Debugger, when run normally the function got called once, with the debugger connected it got called twice.

Upvotes: 0

user4696837
user4696837

Reputation:

You use three Listener 1.OnLongClickListener 2.OnTouchListener 3.OnClickListener

When you click on the View First it call OnTouchListener and then OnClickListener That's why your event fire twice..

Use This Trick I am sure you problem will solve ...... I test it myself

    holder.fiveAction.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            //open activity A after launching an animation of 300ms

            holder.fiveAction.setOnTouchListener(new View.OnTouchListener() {
            @Override
               public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                 // We're only interested in when the button is released.
                 if (event.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    //open activity A even if animation hasn't ended
                    // Do something when the button is released.

                 }
              return false;
             }
            });
            return true;
        }
    });

    holder.fiveAction.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LogUtil.i(TAG, "hi five onClickListener called");
            //open some activity B or do some work
    });

You need to declare holder.fiveAction as final

Upvotes: 4

Related Questions