2

Sorry for the terrible title, I am bad at describing these things.

I am building a metronome and have a (-) UI button that decreases the tempo by 1, and a (+) UI button that increases the tempo by 1.

My problem currently is that whenever I press either buttons, the metronome restarts itself since there's a new tempo, and plays immediately. So if you press the (-) button 10 times in a row, each time you press it you hear the initial metronome "beep".

I would like my app to do the following:

  • When the user clicks either (-) or (+) buttons, wait for 200 milliseconds
  • IF the user didn't click the buttons again in that timeframe, play the metronome
  • If the user DID click the button again, don't play the metronome, repeat the process: wait 200 milliseconds, if no click was made play the metronome, etc

The end result would be that if I'm at 100 bpm and I repeatedly press the (+) button 20 times until I am at 120 bpm, the metronome wouldn't start playing until I am done tapping.

How do I go about implementing this? Thank you!

A. Quest
  • 33
  • 3
  • It sounds like you just described the algorithm you should write to implement what you want it to do. Which part do you not know how to do? – user132278 Mar 17 '16 at 00:28
  • I am new to android and Java development, so it's a bit overwhelming. I am not sure what would be the best/correct way to implement it. – A. Quest Mar 17 '16 at 01:24

2 Answers2

0

If you want a delay between the action and the effect, there are several ways you can achieve it. This is one.

 private boolean pressedAction = false;

@override
public void onClick(View v) {

    if (pressedAction) return;
    pressedAction = true;
    new Thread(new Runnable(
        @override
        public void run() {

            try {
                Thread.sleep(200); // 200 miliseconds
            } catch (Exception e) {}

            // Update views or do work (program logic)

            pressedAction = false;
        }
    }

}

Then, the metronome logic is your bussiness.

Rafael Lucena
  • 635
  • 1
  • 6
  • 9
  • Thanks for the answer. Unless I misunderstood you, I don't think this is what I'm looking for. This seems like a simple delay, but what I'm looking for is a bit more complicated: if the user presses the button 20 times in a row, I want only the very last tap to execute the function - the first 19 taps should do nothing. In this case, if I press the button 20 times in a row, the function still executes 20 times, except with a delay. I hope that makes it clearer. Thanks – A. Quest Mar 17 '16 at 01:23
0

Declare and instantiate the below in your activity:

     private Handler timeoutHandler = new Handler();
     private Runnable delayStartThread = new Runnable() {
                public void run() {
                    startMetronome();
                }
            };

Then insert the below code block in your onClickListener for both + and - buttons:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timeoutHandler.removeCallbacks(delayStartThread);
            tempoOfMetronome++; //tempoOfMetronome--; for decrease button
            stopMetronome();
            timeoutHandler.postDelayed(delayStartThread, 200);
        }
    });

For more details on how the code works, refer the below links for examples (I used these examples to formulate the answer):

  1. Android: clicking TWICE the back button to exit activity - How to use handler.postDelayed()
  2. How to cancel handler.postDelayed? - How to cancel handler.postDelayed()

You should also look at the Android documentation for those methods.

Community
  • 1
  • 1
karansky
  • 538
  • 1
  • 4
  • 24