title: How do I make my full-screen overlay remain full-screen after orientation changes?
body: <p>I am making an app that creates tiny sprite animations that walk around your screen.</p>
  
  <p>I have a main activity, with a button "start service". This starts a service, which (in <code>onCreate()</code>) creates a full-screen view and attaches it to the root window manager.</p>
  
  <p>This part works perfectly. It fills the screen, and you can leave the app, and the animations will still be visible over everything.</p>
  
  <p><a href="https://i.stack.imgur.com/jVAd2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jVAd2.png" alt="initial render is fine"></a></p>
  
  <p>The problem emerges when you rotate the device.</p>
  
  <p><a href="https://i.stack.imgur.com/0xk4o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0xk4o.png" alt="view does not rotate with device"></a></p>
  
  <p><em>Sprites have moved to the middle of the screen, but this is an unrelated issue; the important thing here is the dark bounding box — this box shows you the canvas I am allowed to draw into, and it needs to fill the whole screen</em></p>
  
  <p>The view is still portrait-mode, even though all other layouts seem to have updated correctly.</p>
  
  <hr>
  
  <p>Part of the problem comes from how I have specified the dimensions of the view. I used flags to specify "full screen", but this did <strong>not</strong> set the view's <code>width</code> and <code>height</code> to match the screen's dimensions. Thus I had to set those manually at startup.</p>
  
  <p>I don't know a way to update the width and height of the view once the view is created, but I feel that I need to, since the dimensions of the view determine the dimensions of the canvas.</p>
  
  <p>My service creates the view like so:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class WalkerService extends Service {
      private WalkerView view;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          final WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
  
          final DisplayMetrics metrics = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(metrics);
  
          view = new WalkerView(this); // extends SurfaceView
          final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                  WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, // TYPE_SYSTEM_ALERT is denied in apiLevel &gt;=19
                  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                  PixelFormat.TRANSLUCENT
          );
          view.setFitsSystemWindows(false); // allow us to draw over status bar, navigation bar
  
          // here I _manually_ set the dimensions, because otherwise it defaults to a square (something like 1024x1024)
          params.width = metrics.widthPixels;
          params.height = metrics.heightPixels;
  
          wm.addView(view, params);
      }
  }
  </code></pre>
  
  <p>I have tried listening for orientation changes, and rotating the view when that happens:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
  
      view.setRotation(
              newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
                      ? 90.0f
                      : 0.0f
      );
  }
  </code></pre>
  
  <p>But <code>setRotation()</code> didn't seem to affect <code>width</code> and <code>height</code> of my view, nor did they change the angle at which the canvas was rendered.</p>
  
  <hr>
  
  <p>Am I fundamentally misunderstanding how to create a full-screen overlay? How can I maintain the ability to draw over the entire screen, regardless of orientation (and even when my app is not the active app)?</p>
  
  <p>Maybe it's related to the fact that I attach my view to the Window Service's window manager — perhaps this means my view has an odd parent (I could imagine that maybe the root view would not be affected by orientation, or would have no defined boundaries for children to stretch to fill).</p>
  
  <p>Full source code is <a href="https://github.com/Birch-san/touhou-walk" rel="nofollow noreferrer">public on GitHub</a> in case I've omitted any useful information here.</p>
  
  <p><code>WalkerView.java</code> may be particularly relevant, so <a href="https://github.com/Birch-san/touhou-walk/blob/master/app/src/main/java/uk/co/birchlabs/touhouwalk/services/walker/WalkerView.java" rel="nofollow noreferrer">here it is</a>.</p>