https://developer.android.com/training/system-ui/navigation.html
1 View decorView = getWindow().getDecorView(); 2 // Hide both the navigation bar and the status bar. 3 // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as 4 // a general rule, you should design your app to hide the status bar whenever you 5 // hide the navigation bar. 6 int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 7 | View.SYSTEM_UI_FLAG_FULLSCREEN; 8 decorView.setSystemUiVisibility(uiOptions);
Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
https://developer.android.com/training/system-ui/immersive.html
1 // This snippet hides the system bars. 2 private void hideSystemUI() { 3 // Set the IMMERSIVE flag. 4 // Set the content to appear under the system bars so that the content 5 // doesn't resize when the system bars hide and show. 6 mDecorView.setSystemUiVisibility( 7 View.SYSTEM_UI_FLAG_LAYOUT_STABLE 8 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 9 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 10 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 11 | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar 12 | View.SYSTEM_UI_FLAG_IMMERSIVE); 13 } 14 15 // This snippet shows the system bars. It does this by removing all the flags 16 // except for the ones that make the content appear under the system bars. 17 private void showSystemUI() { 18 mDecorView.setSystemUiVisibility( 19 View.SYSTEM_UI_FLAG_LAYOUT_STABLE 20 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 21 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 22 }
Note: If you like the auto-hiding behavior of IMMERSIVE_STICKY but need to show your own UI controls as well, just use IMMERSIVE combined with Handler.postDelayed() or something similar to re-enter immersive mode after a few seconds.
Current Requirement:
- Hide navigation bar on startup
- Auto hide navigation bar (IMMERSIVE_STICKY + poseDelayed() )
Implementation:
1 ////////////////////////////////////////////////////////////////////////// 2 public void hideNavigationBar() { 3 int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 4 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 5 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 6 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar 7 | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar 8 9 if( android.os.Build.VERSION.SDK_INT >= 19 ){
10 uiFlags |= 0x00001000; //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level 11 } else { 12 uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE; 13 } 14 15 getWindow().getDecorView().setSystemUiVisibility(uiFlags); 16 } 17 18 19 ////////////////////////////////////////////////////////////////////////// 20 @Override 21 protected void onCreate (Bundle savedInstanceState) { 22 ... 23 hideNavigationBar(); 24 super.onCreate(savedInstanceState); 25 ... 26 } 27 28 ////////////////////////////////////////////////////////////////////////// 29 @Override 30 public void onWindowFocusChanged(boolean hasFocus) { 31 super.onWindowFocusChanged(hasFocus); 32 if( hasFocus ) { 33 hideNavigationBar(); 34 } 35 }
Note: postDelayed is not yet added, because on tested devices, navigation bar will auto hide for some secs.
Add postDelayed() event to manully auto hide nav bar if needed.
on some system, after volume key's pressed, navigation bar is shown & never hidden until keys on navi bar is pressed.
hard fix:
1 ////////////////////////////////////////////////////////////////////////// 2 @Override 3 public boolean onKeyUp(int keyCode, KeyEvent event) { 4 super.onKeyUp(keyCode, event); 5 if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){ 6 this.hideNavigationBar(); 7 } 8 return false; 9 }