1 page.title=Responding to UI Visibility Changes 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> 13 <li><a href="#listener">Register a Listener</a></li> 14 </ol> 15 16 17 <!-- other docs (NOT javadocs) --> 18 <h2>You should also read</h2> 19 20 <ul> 21 <li> 22 <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> API Guide 23 </li> 24 <li> 25 <a href="{@docRoot}design/index.html"> 26 Android Design Guide 27 </a> 28 </li> 29 </ul> 30 31 32 </div> 33 </div> 34 35 <p>This lesson describes how to register a listener so that your app can get notified 36 of system UI visibility changes. This is useful if you want to 37 synchronize other parts of your UI with the hiding/showing of system bars.</p> 38 39 <h2 id="listener">Register a Listener</h2> 40 41 <p>To get notified of system UI visibility changes, register an 42 {@link android.view.View.OnSystemUiVisibilityChangeListener} to your view. 43 This is typically the view you are using to control the navigation visibility.</p> 44 45 <p>For example, you could add this code to your activity's 46 {@link android.app.Activity#onCreate onCreate()} method:</p> 47 48 <pre>View decorView = getWindow().getDecorView(); 49 decorView.setOnSystemUiVisibilityChangeListener 50 (new View.OnSystemUiVisibilityChangeListener() { 51 @Override 52 public void onSystemUiVisibilityChange(int visibility) { 53 // Note that system bars will only be "visible" if none of the 54 // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. 55 if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { 56 // TODO: The system bars are visible. Make any desired 57 // adjustments to your UI, such as showing the action bar or 58 // other navigational controls. 59 } else { 60 // TODO: The system bars are NOT visible. Make any desired 61 // adjustments to your UI, such as hiding the action bar or 62 // other navigational controls. 63 } 64 } 65 });</pre> 66 67 <p>It's generally good practice to keep your UI in sync with changes in system bar 68 visibility. For example, you could use this listener to hide and show the action bar in 69 concert with the status bar hiding and showing.</p> 70