0

My code is using 'case and break' example

                    case R.id.student:

                    showMessage("Student");

                    Intent std = new Intent(Home.this, student.class);

                    startActivity(std);

                    break;
A. Garcia
  • 3
  • 3

5 Answers5

1

Try this

switch (menuButton.getId()) {
        case R.id.student:
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showMessage("Student");
                    Intent std = new Intent(Home.this, student.class);
                    startActivity(std);
                }
            }, 2000);
            break;
    }
Munir
  • 2,548
  • 1
  • 11
  • 20
  • How to implement that ? public void onItemClick(CircleMenuButton menuButton) { switch (menuButton.getId()) { case R.id.student: showMessage("Student"); Intent std = new Intent(Home.this, student.class); startActivity(std); break; – A. Garcia Dec 01 '17 at 18:24
  • @A.Garcia happy to help you. you may also accept my answer as right if it helped you – Munir Dec 01 '17 at 18:39
0

Using Runnable:

Handler handler = new Handler();
final Runnable r = new Runnable() {
    public void run() {
        handler.postDelayed(this, 1000);
        // TO DO
    }
};

Using Handler:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // TO DO
            }
        }, 1000);

In Your case it is like:

        switch (menuButton.getId()) {
            case R.id.student:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        showMessage("Student");
                        Intent std = new Intent(Home.this, student.class);
                        startActivity(std);
                    }
                }, 1000);

                break;

        }
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • How to implement that ? public void onItemClick(CircleMenuButton menuButton) { switch (menuButton.getId()) { case R.id.student: showMessage("Student"); Intent std = new Intent(Home.this, student.class); startActivity(std); break; – A. Garcia Dec 01 '17 at 18:25
  • Check my Updated answer – Shailendra Madda Dec 01 '17 at 18:28
0
     public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_cherry) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // do what you want after 1000 miliseconds
                }
            }, 1000);
        }
0

A countdown timer can be used which lies in the object class as follows

 new CountDownTimer(2000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

this will start a timer for 2 seconds and you can handle your desired events in the ontick and onfinish block .

you can further refer to android official documentation

countdown timer

varnit
  • 1,859
  • 10
  • 21
  • Where can I put this case R.id.student: showMessage("Student"); Intent std = new Intent(Home.this, student.class); startActivity(std); break;...using that? – A. Garcia Dec 01 '17 at 18:34
0

Let suppose you have clicked a button at second x (startTime) now you need to enable action performed by button on x+2 second and again for next click x+2+2, for that

long startTime = System.currentTimeMillis();
long difference = System.currentTimeMillis() - startTime;
int seconds=difference/1000; //(this will change milisecond to second 
required value)
if(seconds>2){
//do task
}else{
return; //do not do task or return 
}
// this can be done for one or more than one switches at same time to avaiod multiple click of a button or clicking multiple button at short interval of time

prevent rapid click

Abhishek
  • 16
  • 2