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 ?
⭐ Simply use the keyboard shortcut Ctrl+M to hide the controls or show them again.
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.
private Runnable hideControllerThread = new Runnable() {
public void run() {
volumeBar.setVisibility(View.GONE);
audioControllView.setVisibility(View.GONE);
topBar.setVisibility(View.GONE);
}
};
public void hideControllers() {
hidehandler.postDelayed(hideControllerThread, 15000);
}
public void showControllers() {
volumeBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
audioControllView.setVisibility(View.VISIBLE);
hidehandler.removeCallbacks(hideControllerThread);
hideControllers();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
if (audioControllView.getVisibility() == View.VISIBLE) {
hidehandler.removeCallbacks(hideControllerThread);
hideControllers();
} else {
showControllers();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With