0

During onResume() I want to get the maximum size of content that my Activity can show sans status bar and navigation bar, before anything is shown. Using DisplayMetrics doesn't seem to work as the returned heightPixels seems to include the status bar size:

DisplayMetrics displayMetrics = new DisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels; //includes status-bar size

I've tried getting the Activity's content view however it's not laid out until after onResume() has finished:

View view = this.findViewById(android.R.id.content);
int width = view.getWidth(); // == 0
int height = view.getHeight(); // == 0

I feel like I'm missing something obvious and that it should be simple to get the maximum size that potential content can be before onResume() finishes, or am I wrong?

Rajveer
  • 847
  • 11
  • 28
  • Have a look at this [answer](https://stackoverflow.com/a/5886864/9119277) – Napster Feb 08 '18 at 16:25
  • Unfortunately it looks like `getWindow().findViewById(Window.ID_ANDROID_CONTENT)` returns the same view as `this.findViewById(android.R.id.content)` and so it's leading to the same problem. – Rajveer Feb 08 '18 at 17:29
  • Read the rest of the answer - `Which means onCreate() (and often onResume() also) are too early in the process to do the calculation.` – Napster Feb 08 '18 at 17:36
  • Yep I read that part too, however ultimately I don't really want to know what the actual content view size will be, I want to know what the _possible_ content view size _can_ be, which surely should be known beforehand since system decorations are defined in the theme in the manifest. If I can check whether a status bar is/will be shown, get the frame for it and subtract that from DisplayMetrics's heightPixels this will suffice. Is there really no way to achieve this? – Rajveer Feb 08 '18 at 17:48
  • It depends. When do you require the height? on `onCreate`, `onResponse` or it doesn't matter? – Napster Feb 08 '18 at 17:53
  • Rajveer, what is the actual problem you're trying to solve that causes you to want to know this information? – Ben P. Feb 08 '18 at 17:54
  • @Napster In `onStart` ideally, however in `onResume` would also suffice. – Rajveer Feb 08 '18 at 20:16
  • @BenP. I am tying my NDK-based game engine to the event lifecycle of the activity, and I want it to start being able to create SurfaceViews with WindowManager after it initialises in onStart or onResume. It all works fine, apart from getting the maximum possible content size. – Rajveer Feb 08 '18 at 20:17

0 Answers0