In Fragment
// Hide status bar
View windowDecorView = requireActivity().getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
// Show status bar
View windowDecorView = requireActivity().getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
In Activity
// Hide status bar
View windowDecorView = getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
// Show status bar
View windowDecorView = getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);