title: Android: How to hide the System UI properly
body: <p>I'm developing a video player application and I've faced to a problem. I have a custom video controller which contains a fullscreen button and I want hide the System UI (navigation and status bars) when the user enters to fullscreen mode. I've tried to do something like <a href="https://developer.android.com/training/system-ui/index.html" rel="nofollow noreferrer">this</a>, but it doesn't work properly. My application is like the screenshot below:</p>
  
  <p><a href="https://i.stack.imgur.com/pWSGE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pWSGE.png" alt="enter image description here"></a></p>
  
  <p>When I click the fullscreen button, I change orientation to landscape and I hide the System UI, but my player doesn't get fullscreen. It looks like the screen below:</p>
  
  <p><a href="https://i.stack.imgur.com/U71E9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U71E9.png" alt="Status bar hides and navigation bar hides too, but player doesn&#39;t take the whole screen"></a></p>
  
  <p>Also, when I tap the screen I want to show my video controller, but the System UI shows the below content, instead:</p>
  
  <p><a href="https://i.stack.imgur.com/byFvm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/byFvm.jpg" alt="System UI shows when I tap on screen"></a></p>
  
  <p>So, how can I implement this behavior? Below are my methods for fullscreen and hiding/showing the System UI:</p>
  
  <pre><code>@Override
  public void toggleFullscreen() {
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mVideoContainer.getLayoutParams();
      if (!mFullscreen) {
               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
          hideSystemUI();
          DisplayMetrics metrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(metrics);
          params.width = metrics.widthPixels;
          params.height = metrics.heightPixels;
          params.setMargins(0, 0, 0, 0);
      } else {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          showSystemUI();
          DisplayMetrics metrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(metrics);
          params.width = metrics.widthPixels;
          params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, metrics);
              params.setMargins(0, 0, 0, 0);
      }
      mVideoContainer.setLayoutParams(params);
      mFullscreen = !mFullscreen;
  }
  
  private void hideSystemUI() {
      View decorView = getWindow().getDecorView();
      int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
      if (Build.VERSION.SDK_INT &lt; 16) {
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN)
      } else {
          uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
      }
      decorView.setSystemUiVisibility(uiOptions);
  }
  
  private void showSystemUI() {
      View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(0);
  }
  </code></pre>
  
  <p>Here's my layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/video_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;FrameLayout
          android:id="@+id/videoSurfaceContainer"
          android:layout_width="match_parent"
          android:layout_height="300dp" &gt;
  
          &lt;SurfaceView
              android:id="@+id/videoSurface"
              android:layout_width="match_parent"
              android:layout_height="match_parent" /&gt;
      &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>