Do NOT use an onClickListener in this case.
It is solved with this code
View contentView = findViewById(R.id.contentLayout);
contentView.setClickable(true);
final GestureDetector clickDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
boolean visible = (mDecorView.getSystemUiVisibility()
& View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
if (visible)
hideSystemUI();
else
showSystemUI();
return true;
}
});
contentView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return clickDetector.onTouchEvent(event);
}
});
and the functions to go/hide the Immersive Full-Screen Mode:
private void hideSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
I found out about this problem in a video of Roman Nurik here
https://developer.android.com/training/system-ui/immersive.html