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