Katie McCormick | cfd3047 | 2013-09-27 15:52:14 -0700 | [diff] [blame] | 1 | page.title=Hiding the Navigation Bar |
| 2 | |
| 3 | trainingnavtop=true |
| 4 | |
| 5 | @jd:body |
| 6 | |
| 7 | <div id="tb-wrapper"> |
| 8 | <div id="tb"> |
| 9 | |
| 10 | <!-- table of contents --> |
| 11 | <h2>This lesson teaches you to</h2> |
| 12 | <ol> |
Katie McCormick | 467f5db | 2013-11-05 13:35:15 -0800 | [diff] [blame] | 13 | <li><a href="#40">Hide the Navigation Bar on 4.0 and Higher</a></li> |
Katie McCormick | cfd3047 | 2013-09-27 15:52:14 -0700 | [diff] [blame] | 14 | <li><a href="#behind">Make Content Appear Behind the Navigation Bar</a></li> |
| 15 | </ol> |
| 16 | |
| 17 | |
| 18 | <!-- other docs (NOT javadocs) --> |
| 19 | <h2>You should also read</h2> |
| 20 | |
| 21 | <ul> |
| 22 | <li> |
| 23 | <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> API Guide |
| 24 | </li> |
| 25 | <li> |
| 26 | <a href="{@docRoot}design/index.html"> |
| 27 | Android Design Guide |
| 28 | </a> |
| 29 | </li> |
| 30 | </ul> |
| 31 | |
Katie McCormick | 467f5db | 2013-11-05 13:35:15 -0800 | [diff] [blame] | 32 | <h2>Try it out</h2> |
| 33 | |
| 34 | <div class="download-box"> |
| 35 | <a href="{@docRoot}samples/ImmersiveMode/index.html" |
| 36 | class="button">Get the sample</a> |
| 37 | <p class="filename">ImmersiveMode sample</p> |
| 38 | </div> |
Katie McCormick | cfd3047 | 2013-09-27 15:52:14 -0700 | [diff] [blame] | 39 | |
| 40 | </div> |
| 41 | </div> |
| 42 | |
| 43 | <p>This lesson describes how to hide the navigation bar, which was introduced in |
| 44 | Android 4.0 (API level 14).</p> |
| 45 | |
| 46 | <p>Even though this lesson focuses on hiding the |
| 47 | navigation bar, you should design your app to hide the status bar |
| 48 | at the same time, as described in <a href="status.html">Hiding the Status Bar</a>. |
| 49 | Hiding the navigation and status bars (while still keeping them readily accessible) |
| 50 | lets the content use the entire display space, thereby providing a more immersive |
| 51 | user experience. </p> |
| 52 | |
| 53 | <img src="{@docRoot}images/training/navigation-bar.png" |
| 54 | alt="system bars"> |
| 55 | <p class="img-caption"><strong>Figure 1.</strong> Navigation bar.</p> |
| 56 | |
| 57 | |
| 58 | |
| 59 | <h2 id="40">Hide the Navigation Bar on 4.0 and Higher</h2> |
| 60 | |
| 61 | <p>You can hide the navigation bar on Android 4.0 and higher using the |
| 62 | {@link android.view.View#SYSTEM_UI_FLAG_HIDE_NAVIGATION} flag. This snippet hides both |
| 63 | the navigation bar and the status bar:</p> |
| 64 | <pre>View decorView = getWindow().getDecorView(); |
| 65 | // Hide both the navigation bar and the status bar. |
Katie McCormick | 8b8194b | 2013-12-12 14:42:55 -0800 | [diff] [blame] | 66 | // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as |
| 67 | // a general rule, you should design your app to hide the status bar whenever you |
| 68 | // hide the navigation bar. |
Katie McCormick | cfd3047 | 2013-09-27 15:52:14 -0700 | [diff] [blame] | 69 | int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
| 70 | | View.SYSTEM_UI_FLAG_FULLSCREEN; |
| 71 | decorView.setSystemUiVisibility(uiOptions);</pre> |
| 72 | |
| 73 | <p>Note the following:</p> |
| 74 | |
| 75 | <ul> |
| 76 | <li>With this approach, touching anywhere on the screen causes the navigation bar (and |
| 77 | status bar) to reappear and remain visible. The user interaction causes the flags to be |
| 78 | be cleared.</li> |
| 79 | <li>Once the flags have been cleared, your app needs to reset them if you |
| 80 | want to hide the bars again. See <a href="visibility.html">Responding to UI Visibility Changes</a> for a |
| 81 | discussion of how to listen for UI visibility changes so that your app can |
| 82 | respond accordingly.</li> |
| 83 | |
| 84 | <li>Where you set the UI flags makes a difference. If you hide the system bars in your activity's |
| 85 | {@link android.app.Activity#onCreate onCreate()} method and the user presses Home, the system bars will |
| 86 | reappear. When the user reopens the activity, {@link android.app.Activity#onCreate onCreate()} |
| 87 | won't get called, so the system bars will remain visible. If you want system UI changes to |
| 88 | persist as the user navigates in and out of your activity, set UI flags in |
| 89 | {@link android.app.Activity#onResume onResume()} |
| 90 | or {@link android.view.Window.Callback#onWindowFocusChanged onWindowFocusChanged()}.</li> |
| 91 | |
| 92 | <li>The method {@link android.view.View#setSystemUiVisibility setSystemUiVisibility()} only |
| 93 | has an effect if the view you call it from is visible.</li> |
| 94 | <li>Navigating away from the view causes flags |
| 95 | set with {@link android.view.View#setSystemUiVisibility setSystemUiVisibility()} |
| 96 | to be cleared.</li> |
| 97 | </ul> |
| 98 | |
| 99 | <h2 id="behind">Make Content Appear Behind the Navigation Bar</h2> |
| 100 | <p>On Android 4.1 and higher, you can set your application's content to appear behind |
| 101 | the navigation bar, so that the content doesn't resize as the navigation bar hides and |
| 102 | shows. To do this, use |
Katie McCormick | 467f5db | 2013-11-05 13:35:15 -0800 | [diff] [blame] | 103 | {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}. |
Katie McCormick | cfd3047 | 2013-09-27 15:52:14 -0700 | [diff] [blame] | 104 | You may also need to use |
| 105 | {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} to help your app maintain a |
| 106 | stable layout.</p> |
| 107 | |
| 108 | <p>When you use this approach, it becomes your responsibility to ensure that critical parts |
| 109 | of your app's UI don't end up getting covered by system bars. For more |
| 110 | discussion of this topic, see the <a href="status.html#behind"> |
| 111 | Hiding the Status Bar</a> lesson.</p> |