I want to set a booleanVariable in my project using delay. So basically I have a boolean variable which is set to true when user touches a button. And after two seconds that boolean variable should be set back to false. So my question is how do I introduce that delay of 2 seconds?
I have following code which DID NOT work-
boolean userTouch;
public void buttonTouched(){//This gets called when user touches the button
setUserTouch(true);
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace;
}
setUserTouch(false);
}
In another method I call getUserTouch()
method to see if user has touched the button in past 2 seconds. But that method is always set to false. It's never true.
Is there something wrong with my code? should the setUserTouch(true);
method be inside the try block?
Also is there any other wayI can accomplish my goal? i.e. any other method by which I can introduce 2 second delay. I'm trying out countdown timer now. But I don't know any other method.