Class DynamicWindowUtils


  • public class DynamicWindowUtils
    extends java.lang.Object
    Helper class to perform window operations and to detect system configurations at runtime.
    See Also:
    WindowManager
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static android.graphics.Point getAppUsableScreenSize​(android.content.Context context)
      Get the app usable screen size.
      static android.view.WindowMetrics getCurrentWindowMetrics​(android.content.Context context)
      Returns the current window metrics for the supplied context.
      static android.view.Display getDisplay​(android.content.Context context)
      Returns the correct display according to the different API levels.
      static float getDisplayDensity​(android.content.Context context)
      Returns the display used by the supplied context.
      static android.util.DisplayMetrics getDisplayMetrics​(android.content.Context context)
      Returns the display metrics for the supplied context.
      static android.graphics.Point getNavigationBarSize​(android.content.Context context)
      Get the on-screen navigation bar size.
      static int getOverlayFlag​(boolean alert)
      Get the overlay flag according to the Android version.
      static int getOverlayFlag​(boolean alert, boolean accessibility)
      Get the overlay flag according to the Android version.
      static android.graphics.Point getRealScreenSize​(android.content.Context context)
      Get the real screen size.
      static int getScreenOrientation​(android.content.Context context)
      Get the current device orientation.
      static int getStatusBarSize​(android.content.Context context)
      Get the status bar size.
      static boolean isGestureNavigation​(android.content.Context context)
      Detects support for gesture navigation.
      static boolean isNavigationBarPresent​(android.content.Context context)
      Detects if on-screen navigation bar is present or not.
      static boolean isNavigationBarThemeSupported​(android.content.Context context)
      Detects support for navigation bar theme.
      static void setEdgeToEdge​(android.view.Window window, boolean edgeToEdge)
      Set the hide navigation flag for edge-to-edge content on API 23 and above.
      static void setShowWallpaper​(android.view.Window window, boolean showWallpaper)
      Set the show wallpaper flag for the supplied window.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DynamicWindowUtils

        public DynamicWindowUtils()
    • Method Detail

      • getDisplay

        @Nullable
        public static android.view.Display getDisplay​(@Nullable
                                                      android.content.Context context)
        Returns the correct display according to the different API levels.
        Parameters:
        context - The context to be used.
        Returns:
        The display according to the API level.
        See Also:
        Context.getDisplay(), WindowManager.getDefaultDisplay()
      • getDisplayMetrics

        @Nullable
        public static android.util.DisplayMetrics getDisplayMetrics​(@Nullable
                                                                    android.content.Context context)
        Returns the display metrics for the supplied context.
        Parameters:
        context - The context to be used.
        Returns:
        The display metrics for the supplied context.
        See Also:
        Context.getResources(), Resources.getDisplayMetrics()
      • getCurrentWindowMetrics

        @Nullable
        public static android.view.WindowMetrics getCurrentWindowMetrics​(@Nullable
                                                                         android.content.Context context)
        Returns the current window metrics for the supplied context.
        Parameters:
        context - The context to be used.
        Returns:
        The current window metrics for the supplied context.
        See Also:
        WindowManager.getCurrentWindowMetrics()
      • getDisplayDensity

        public static float getDisplayDensity​(@Nullable
                                              android.content.Context context)
        Returns the display used by the supplied context.
        Parameters:
        context - The context to be used.
        Returns:
        The display density used by the supplied context.
        See Also:
        Context.getResources(), Resources.getDisplayMetrics(), DisplayMetrics.density
      • getAppUsableScreenSize

        @NonNull
        public static android.graphics.Point getAppUsableScreenSize​(@Nullable
                                                                    android.content.Context context)
        Get the app usable screen size.
        Parameters:
        context - The context to be used.
        Returns:
        The app usable screen size in point.
        See Also:
        Context.WINDOW_SERVICE, Point
      • getRealScreenSize

        @NonNull
        public static android.graphics.Point getRealScreenSize​(@Nullable
                                                               android.content.Context context)
        Get the real screen size.
        Parameters:
        context - The context to be used.
        Returns:
        The real screen size in point.
        See Also:
        Context.WINDOW_SERVICE, Point
      • getStatusBarSize

        public static int getStatusBarSize​(@NonNull
                                           android.content.Context context)
        Get the status bar size.
        Parameters:
        context - The context to get the resources.
        Returns:
        The status bar size in pixels.
      • getNavigationBarSize

        @NonNull
        public static android.graphics.Point getNavigationBarSize​(@NonNull
                                                                  android.content.Context context)
        Get the on-screen navigation bar size.
        Parameters:
        context - The context to be used.
        Returns:
        The on-screen navigation bar size in point.
        See Also:
        Context.WINDOW_SERVICE, Point
      • isNavigationBarPresent

        public static boolean isNavigationBarPresent​(@NonNull
                                                     android.content.Context context)
        Detects if on-screen navigation bar is present or not.
        Parameters:
        context - The context to retrieve the resources.
        Returns:
        true if on-screen navigation bar is present.
      • isNavigationBarThemeSupported

        public static boolean isNavigationBarThemeSupported​(@NonNull
                                                            android.content.Context context)
        Detects support for navigation bar theme.
        Parameters:
        context - The context to retrieve the resources.
        Returns:
        true if navigation bar theme is supported.
      • isGestureNavigation

        public static boolean isGestureNavigation​(@NonNull
                                                  android.content.Context context)
        Detects support for gesture navigation.
        Parameters:
        context - The context to retrieve the resources.
        Returns:
        true if gesture navigation is supported.
      • setShowWallpaper

        public static void setShowWallpaper​(@Nullable
                                            android.view.Window window,
                                            boolean showWallpaper)
        Set the show wallpaper flag for the supplied window.

        This method must be called before window is created.

        Parameters:
        window - The window to be used.
        showWallpaper - true to show the wallpaper.
        See Also:
        WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER
      • setEdgeToEdge

        public static void setEdgeToEdge​(@Nullable
                                         android.view.Window window,
                                         boolean edgeToEdge)
        Set the hide navigation flag for edge-to-edge content on API 23 and above.
        Parameters:
        window - The window to be used.
        edgeToEdge - true to hide the layout navigation.
      • getScreenOrientation

        public static int getScreenOrientation​(@Nullable
                                               android.content.Context context)
        Get the current device orientation.
        Parameters:
        context - The context to be used.
        Returns:
        The current activity orientation info.
        See Also:
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
      • getOverlayFlag

        public static int getOverlayFlag​(boolean alert,
                                         boolean accessibility)
        Get the overlay flag according to the Android version.
        Parameters:
        alert - true to return alert flag on API 25 and below.
        accessibility - true to return the accessibility overlay.
        Returns:
        The WindowManager overlay flag according to the Android version.
        See Also:
        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY
      • getOverlayFlag

        public static int getOverlayFlag​(boolean alert)
        Get the overlay flag according to the Android version.
        Parameters:
        alert - true to return alert flag on API 25 and below.
        Returns:
        The WindowManager overlay flag according to the Android version.
        See Also:
        getOverlayFlag(boolean, boolean), WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT