1

public class Main2Activity extends AppCompatActivity {
int square5 = 0;
int flag = 1;
int userclick5 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    //here i set some click listeners. one example is as follows:
    ImageButton image_button1 = (ImageButton) findViewById(R.id.image_button1);
    image_button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            goToIB();
        }
    });
}
//click listener call this function. This function set the color of image button as green.
//Then it calls the function goTofirst3cpu()
    private int goToIB() {
    ImageButton imageButton5 = (ImageButton) findViewById(R.id.image_button5);
    if (imageButton5.isPressed() & square5 == 0) {
        {
            imageButton5.setImageResource(R.color.green_color);
            flag = flag + 1;
            square5 = square5 + 1;
            userclick5 = userclick5 + 1;
            goTofirst3cpu();
            return square5;
        }
    }
    return 0;
    }
    //This function shows red color in image button
    private void goTofirst3cpu() {
    ImageButton imageButton5 = (ImageButton) findViewById(R.id.image_button5);

    if (square5 == 0) {

        //HERE IS WHAT I NEED TO DO SOMETHING
        //when i do something, there will be a gap of 5 seconds 
        //before changing imagebutton's color from green to red.
        //?????????  WHAT I WILL DO HERE.  ??????

        imageButton5.setImageResource(R.color.red_color);
        square5 = square5 + 1;
    }

    }
    }

I used Toast and all that stuff. Also system clock. But those things doesn't work for me. I need a break of 5 seconds before changing the image button's color to red. I am a newbie in programming. So explain in simple language. Thanks in advance.....

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Richter
  • 11
  • 2
  • Have you tried `Thread.sleep(5000);`? Or do your really want to stop **ALL** processes?? – ParkerHalo Jun 23 '16 at 17:42
  • 1
    Thankfully, it's impossible to stop ALL processes. – Krythic Jun 23 '16 at 17:53
  • @parkerHalo yes, i tried it.But that is not i am searching for. Yeah.. i really want to stop ALL processes.Which means when the user press the next button it will not be work, till the 5 seconds over. – Richter Jun 24 '16 at 10:56

2 Answers2

1

Solution:

try
{
  Thread.sleep(//milliseconds);
}
catch(InterruptedException e)
{
  //handles exception
}

Thread.sleep() will pause your program for a certain amount of time. It is recommended that you catch the Exception and handle it properly as it can create more problems for your program...

Info on the Interrupted Exception Here in this repeat question:

When does Java's Thread.sleep throw InterruptedException?

Another Solution if Thread.sleep() is a Problem..

 int delay = 1000; //milliseconds
   ActionListener taskPerformer = new ActionListener() {
       int x = 0;
   public void actionPerformed(ActionEvent evt) {

           if(x == 5)
           {
              //change color of button...
           }

           x++;
       }
   };
   new Timer(delay, taskPerformer).start();

What you can do is implement a Swing Timer within your program. You would create a Timer object to fire an action every 1000 milliseconds. Have a variable within the timer that increments every second(basically every time the action is fired) and do something when that variable is equal to five. You can also just set the Timer to 5000 milliseconds instead of 1000 as a parameter... Point is, after a certain amount of time/ do something.

Community
  • 1
  • 1
DarkV1
  • 1,776
  • 12
  • 17
  • yeah,....I tried this earlier, it works. But the problem is that this code works a little bit earlier. Which means, in our example (in the given question) both colors displayed after 5 seconds.we need to display green image then pause for 5 seconds then display the red. – Richter Jun 24 '16 at 11:43
  • @Richter i think swing timer will work... Edited solution – DarkV1 Jun 24 '16 at 17:51
0

You do all the work in main thread, so you cant just use Thread.sleep (), because app will hang. Try to use Handler.postDelayed () instead on main thread or new thread and then do all the stuff- change color or something else. Another (more complex an powerful) solution - use rx library (Timer)

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11