title: How to find dimensions for status bar, navigation bar and screen in Android AccessibilityService
body: <p>I have a Java app (API 23). In it I have a <code>MainActivity</code> and an <code>AccessibilityService</code>. In the AccessibilityService:</p>
  <pre><code>@Override
  public void onServiceConnected() {
      oView = new LinearLayout(this);
      oView.setBackgroundColor(0x88ff0000); // The translucent red color
      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              width,
              height,
              xPos, yPos,
              WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
              WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                      WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                      WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
              PixelFormat.TRANSLUCENT
      );
      WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
      wm.addView(oView, params);
  } 
  </code></pre>
  <p>I need to adjust the <code>width</code>, <code>height</code>, <code>xPos</code> and <code>yPos</code> so it covers the whole screen including the status bar and navigation bar. That means I have to find the dimensions for the status bar, screen, and navigation bar (I can work out the values I need once I have those).</p>
  <p>I found <a href="https://stackoverflow.com/questions/3355367/height-of-statusbar/3356263">Height of statusbar?</a> - best answer seemed to be</p>
  <pre><code>ViewCompat.setOnApplyWindowInsetsListener(rootView, new OnApplyWindowInsetsListener() {
                  @Override
                  public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                      //THIS is the value you want.
                      int statusBarHeight = insets.getSystemWindowInsetTop();
                      int navigationBar = insets.getSystemWindowInsetBottom();
  
                      // Let the view handle insets as it likes.
                      return ViewCompat.onApplyWindowInsets(v, insets);
                  }
              });
  </code></pre>
  <p>But what <code>rootView</code> can I use, since there's no guarantee my Activity will be visible? And do I have to move the creation of the view from <code>onServiceConnected()</code> to inside <code>onApplyWindowInsets()</code>?</p>
  <p>Is this really the best way to do this?</p>
  <p>Follow up questions to bear in mind for the above - depending on the above and whether it answers them too, I will ask here or in a separate question:</p>
  <p>What happens when the screen rotates? What if the status bar and/or navigation bar disappear (for example, if it goes fullscreen)? Seems like I need to handle that, re-read the heights, and re-create the view. Any thoughts or guidance on how to do that would be welcomed.</p>
  <p>=== Update - Progress so far ===</p>
  <p>I thought I only needed screen size plus info about the status bar (size, location), but Navigation Bar is sometimes on the left side of the screen so I need that, and Keyboard might affect things too.</p>
  <p>I can get screen size with</p>
  <pre><code>    Display myDisplay = wm.getDefaultDisplay();
      Display.Mode mode = myDisplay.getMode();
      int height = mode.getPhysicalHeight();
      int width = mode.getPhysicalWidth();
  </code></pre>
  <p>I'm still working on the various location and sizes. I tried creating a view and then reading insets but it gave me the wrong answers. So unless a better answer arises I will probably have to use</p>
  <pre><code>    int result = 0;
      int resourceId = getResources().getIdentifier(&quot;status_bar_height&quot;, &quot;dimen&quot;, &quot;android&quot;);
      if (resourceId &gt; 0) {
          result = getResources().getDimensionPixelSize(resourceId);
      }
  </code></pre>
  <p>But that can give wrong answers in some cases if Status bar is hidden, and it doesn't tell me where on the screen it is. I'm still working on that; got a few things to try, so we'll see.</p>
  <p>Handling rotation in a service can be done using onConfigurationChanged() - see <a href="https://stackoverflow.com/questions/4625167/how-do-i-use-a-service-to-monitor-orientation-change-in-android">How do I use a service to monitor Orientation change in Android</a></p>
  <p>Hope that helps someone - please, desperately searching for those final pieces.</p>