I am designing a media player with a custom layout. I want the interface to disappear after 16s of inactivity. It should reappear if the user touches the screen. The code snippet is given below:
public void showhideControllers(int n) {
if (n == 1) {
/* make layout invisible */
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
volumeBar.setVisibility(View.INVISIBLE);
audioControllView.setVisibility(View.INVISIBLE);
topBar.setVisibility(View.INVISIBLE);
}
}, 16000);
} else {
/* make layout visible */
volumeBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
audioControllView.setVisibility(View.VISIBLE);
showhideControllers(1);
}
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
showhideControllers(2);
}
Inside the onCreate(), I am starting the timer by calling showhideControllers(1);.
Now, when I click on the screen the layout reappears and the timer is reset. But if I randomly go on clicking the screen the timer is not reset after every click and the layout disappears after 16s.
Can you tell me what am I doing wrong ?
Sorry for late response. But here is the solution. I was having similar issue. So I have made following changes in your code, please try this and let me know if it helps you.