Roval
Roval

Reputation: 597

OnTouchListener() doesn't work without onClickListener()

I'm facing a problem that I don't know how to solve and why all of this is happening.

It's simple. I've got a ImageView working as a button and when user click it something is moving on the screen and when the touch is realeased it stops.

So I've just set OnTouchListener on my ImageView and hoped it works but it doesn't.

I need to implement empty OnClickListener and then it's doing what it should do.

private void setSolveImage() {
    ImageView solve_button = (ImageView)getView().findViewById(R.id.solve_button);

    solve_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
        }
    });

    solve_button.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                    screen.setTilesOrder(status.getCleanStatus()); // do something with screen
            } else if (action == MotionEvent.ACTION_UP) {
                    screen.setTilesOrder(status.getGameStatus()); // return screen to previous state
            }
        }
        return false;
        }
    });
}

Can you tell me please what am I doing wrong? Why do I have to do this this way?

Upvotes: 0

Views: 804

Answers (2)

user1517638
user1517638

Reputation: 982

return true if the callback consumed on touch, false otherwise. so change the return type as true instead of false in setOnTouchListener.

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Just do one change and do a trick

do return true; instead return false; in setOnTouchListener()

No need to set setOnClickListener()

Upvotes: 3

Related Questions