0

How to create a java code for android that waits for some time , like 3 - 5 secs for the user to perform a button click which just changes its xy coordinates and increments a score , else intents to another activity.

I have tried to implement it by referring to this SO answer [ How to pause / sleep thread or process in Android? ,but the code keeps intenting to another activity after 5-6 button clicks even if i click within the specified delay time. This is the code -

btn.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View v) 
{
           score++;
            scr=String.valueOf(score);
            scoredisp.setText(scr);
           int xval=xvalue();
            int yval=yvalue();
            btn.setX(xval);
            btn.setY(yval);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() 
{
                    Intent result = new  Intent(MainActivity.this,com.example.warlock.buttonsquash.result.class);
                    startActivity(result);
                }
            }, 3000);

        }
    });


int xvalue() {
    Random randomGenerator = new Random();
    int xval = randomGenerator.nextInt(850) + 1;
    return xval;
}

int yvalue() {
    Random randomGenerator = new Random();
    int yval = randomGenerator.nextInt(1500) + 1;
    return yval;
}

I just need button click to change the button coordinates and increment a score value but the intent starts happening altogether after 5-6 button-clicks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

0

First make a timer(Handler) for 3 or 5 seconds. When they pass check if boolean for button clicked is true else redirect to another activity. Hope I helped you.

MC Star
  • 18
  • 6
  • Please, elaborate your answer with simple example for better understanding of your approach. @MCStar – MS90 Apr 13 '19 at 17:34
  • I enclosed the whole intent code inside an if(!clicked) loop and made clicked=true just after the btn.setY(yval). But the code now does not perform intent even after the specific time delay, ie 3 secs. – Alfn William Apr 13 '19 at 17:41
0

This code will let user wait in current activity for 5 seconds. User can click the button at that time, after that the user will be intented to another activity.

Use this code inside your onCreate()

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 5sec.
   // Intent to other activity from there.
  }
}, 5000);

Hope this help you.

Gulshan Yadav
  • 424
  • 5
  • 13
0

Pls try something like this:

        findViewById(R.id.theButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                theButtonWasClicked = true;
            }
        });

        final int FIVE_SECONDS = 5000;

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (theButtonWasClicked) {
                    Log.d("TheTag", "5 seconds passed with button clicked!");
                    theButtonWasClicked = false;
                } else {
                    Log.d("TheTag", "5 seconds passed with NO button click!");
                    timer.cancel();
                }
            }
        }, FIVE_SECONDS, FIVE_SECONDS);

It will execute your desired action if user did not click within 5 seconds, or it will continue to wait until next 5 seconds interval with no click (is that what you want?)

ror
  • 3,295
  • 1
  • 19
  • 27
0

I have found a work-around myself. You could have understood from the question that the original problem was that multiple handlers delays would cause multiple intents after 5-6 consecutive button-clicks. I referenced this SO answer [ How to change/reset handler post delayed time? ] and coded a stop() and restart() fn and it fixed my error.

Attaching final code for reference-

public class MainActivity extends AppCompatActivity {

Button btn; int score; String scr; TextView scoredisp; boolean click;
public static Handler myHandler = new Handler();
private static final int TIME_TO_WAIT = 700;
Runnable myRunnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn=(Button) findViewById(R.id.imgbtn);
    scoredisp=(TextView)findViewById(R.id.score);
    click=false;


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stop();
            score++;
            scr=String.valueOf(score);
            scoredisp.setText(scr);

            Random random = new Random();
           int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
           btn.setBackgroundColor(color);

           int xval=xvalue();
            int yval=yvalue();
            //int butang=butangle();
           // btn.setRotation(butang);
            btn.setX(xval);
            btn.setY(yval);
            click=false;
            checkvalue();
            restart();
        }

    });



}

 int xvalue() {
    Random randomGenerator = new Random();
    int xval = randomGenerator.nextInt(850) + 1;
    return xval;
}

int yvalue() {
    Random randomGenerator = new Random();
    int yval = randomGenerator.nextInt(1500) + 1;
    return yval;
}

int butangle() {
    Random randomGenerator = new Random();
    int butang = randomGenerator.nextInt(360) + 1;
    return butang;
}


public void checkvalue() {
    myRunnable = new Runnable() {
        @Override
        public void run() {
            if(!click) {
                Intent result = new Intent(MainActivity.this, com.example.warlock.buttonsquash.result.class);
                startActivity(result);
            }
        }
    };

}

public void start() {
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}

public void stop() {
    myHandler.removeCallbacks(myRunnable);
}

public void restart() {
    myHandler.removeCallbacks(myRunnable);
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}
}

Thank you all for your response.