madcoderz
madcoderz

Reputation: 4431

problem with onclick android

i've been searching the web but i find no answer to my question. I have a button, when you press it, it will play a sound. The problem is that when you touch a button on the screen it goes to the onClickListener() only after the button have been released. I need it to run the listener when the button is pressed not when it's released, because this cause a delay when playing the sound. I tried onTouchListener() and it didn't work either, because the sound get's played every time i move the finger over the button. I tried onKeyDown() but it won't work for screen buttons. Any ideas? Some help will be appreciated.

Thanks

Upvotes: 1

Views: 271

Answers (2)

Zarah
Zarah

Reputation: 5189

Maybe you can still use your implementation for onTouchListener(). Just set a boolean flag for when the user starts touching the button, then while it is set true (meaning the user hasn't released the button yet) do not play the sound. When the user releases the button (ACTION_UP), set the flag back to false. This would mean you are ready to play the sound again.

Upvotes: 0

Patrick
Patrick

Reputation: 3759

You can use OnTouchListener and test the event action:

public boolean onTouch (View v, MotionEvent event) {
  if (event.getAction () == MotionEvent.ACTION_DOWN) { // ...
  }
}

Upvotes: 2

Related Questions