I set up event listener, for example: setOnClickListener like this
Button stopBtn = (Button)findViewById(R.id.stop);
stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doMagic();
}
});
I would like to set this listener a timeout event on 10s if button is not pressed. Use case: i have button1 that activates this stopBtn listener for 10s and if timeout comes it becomes deactivated and i need to press button1 to make stopBtn active again.
Im probably doing it wrong:
final Handler myHandler = new Handler();
startBtn = (Button)findViewById(R.id.start);
myHandler.postDelayed(new Runnable() {
public void run() {
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG,"runned");
}
});
}
}, 10000);
After 10s im still able to click it and that is probably cos event listener is still attached. How can i detach it even if i don’t know if its fired or not.
A delayed Runnable posted on a Handler could manage that:
You can init the Handler as an instance variable by using this code: