Class DynamicViewUtils


  • public class DynamicViewUtils
    extends java.lang.Object
    Helper class to perform View operations.
    • Constructor Detail

      • DynamicViewUtils

        public DynamicViewUtils()
    • Method Detail

      • isRootLayout

        public static <T extends View> boolean isRootLayout​(@Nullable
                                                            T view)
        Checks if the supplied view is the only root layout in the view heirarchy.
        Type Parameters:
        T - The type of the view.
        Parameters:
        view - The view to be checked.
        Returns:
        true if the supplied view is the only root layout in the view heirarchy.
      • setEdgeToEdge

        public static void setEdgeToEdge​(@Nullable
                                         View view,
                                         boolean edgeToEdge)
        Deprecated.
        Use DynamicWindowUtils.setEdgeToEdge(Window, boolean) to support latest API levels.
        Set the hide navigation flag for edge-to-edge content on API 23 and above.
        Parameters:
        view - The view to get the system ui flags.
        edgeToEdge - true to hide the layout navigation.
      • isEdgeToEdge

        public static boolean isEdgeToEdge​(@NonNull
                                           View view)
        Deprecated.
        Try to use WindowInsetsControllerCompat if possible.
        Checks whether the hide navigation flag is enabled for edge-to-edge content on API 23 and above.
        Parameters:
        view - The view to get the system ui flags.
        Returns:
        true if hide navigation flag is enabled for edge-to-edge content on API 23 and above.
      • setLightStatusBar

        public static void setLightStatusBar​(@Nullable
                                             Window window,
                                             @Nullable
                                             View view,
                                             boolean light)
        Set light status bar on API 23 and above.
        Parameters:
        window - The window to which the view is attached.
        view - The view to get the system ui flags.
        light - true to set the light status bar.
        See Also:
        setLightStatusBar(View, boolean)
      • setLightStatusBar

        public static void setLightStatusBar​(@Nullable
                                             View view,
                                             boolean light)
        Set light status bar on API 23 and above.
        Parameters:
        view - The view to get the system ui flags.
        light - true to set the light status bar.
        See Also:
        setLightStatusBarLegacy(View, boolean)
      • setLightStatusBarLegacy

        public static void setLightStatusBarLegacy​(@Nullable
                                                   View view,
                                                   boolean light)
        Set light status bar by using the legacy method on API 23 and above.
        Parameters:
        view - The view to get the system ui flags.
        light - true to set the light status bar.
      • setLightNavigationBar

        public static void setLightNavigationBar​(@Nullable
                                                 Window window,
                                                 @Nullable
                                                 View view,
                                                 boolean light)
        Set light navigation bar on API 26 and above.
        Parameters:
        window - The window to which the view is attached.
        view - The view to get the system ui flags.
        light - true to set the light navigation bar.
        See Also:
        setLightNavigationBar(View, boolean)
      • setLightNavigationBar

        public static void setLightNavigationBar​(@Nullable
                                                 View view,
                                                 boolean light)
        Set light navigation bar on API 26 and above.
        Parameters:
        view - The view to get the system ui flags.
        light - true to set the light navigation bar.
        See Also:
        setLightNavigationBarLegacy(View, boolean)
      • setLightNavigationBarLegacy

        public static void setLightNavigationBarLegacy​(@Nullable
                                                       View view,
                                                       boolean light)
        Set light navigation bar on API 26 and above.
        Parameters:
        view - The view to get the system ui flags.
        light - true to set the light navigation bar.
      • addView

        public static void addView​(@Nullable
                                   ViewGroup viewGroup,
                                   @Nullable
                                   View view,
                                   boolean removePrevious)
        Add a view to the view group.
        Parameters:
        viewGroup - The view group to add the view.
        view - The view to be added.
        removePrevious - true to remove all the previous views of the view group.
      • addView

        public static void addView​(@Nullable
                                   ViewGroup viewGroup,
                                   @LayoutRes
                                   int layoutRes,
                                   boolean removePrevious)
        Add a view to the view group.
        Parameters:
        viewGroup - The view group to add the view.
        layoutRes - The layout resource to be added.
        removePrevious - true to remove all the previous views of the view group.
      • manageScrollIndicators

        public static void manageScrollIndicators​(@Nullable
                                                  View view,
                                                  @Nullable
                                                  View upIndicator,
                                                  @Nullable
                                                  View downIndicator)
        Manage scroll indicators for a view according to its current state.
        Parameters:
        view - The view to show or hide the scroll indicators accordingly.
        upIndicator - The indicator to show if the view can be scrolled upwards.
        downIndicator - The indicator to show if the view can be scrolled downwards.
      • isLayoutRtl

        public static boolean isLayoutRtl​(@Nullable
                                          View view)
        Checks whether the layout direction is RTL (right-to-left).
        Parameters:
        view - The view to be used.
        Returns:
        true if the layout direction is RTL (right-to-left).
        See Also:
        ViewCompat.getLayoutDirection(View), TextUtilsCompat.getLayoutDirectionFromLocale(Locale), ViewCompat.LAYOUT_DIRECTION_RTL
      • applyWindowInsets

        public static void applyWindowInsets​(@Nullable
                                             View view,
                                             boolean left,
                                             boolean top,
                                             boolean right,
                                             boolean bottom,
                                             boolean consume)
        Apply window insets padding for the supplied view.
        Parameters:
        view - The view to set the insets padding.
        left - true to apply the left window inset padding.
        top - true to apply the top window inset padding.
        right - true to apply the right window inset padding.
        bottom - true to apply the bottom window inset padding.
        consume - true to consume the applied window insets.
      • applyWindowInsetsHorizontal

        public static void applyWindowInsetsHorizontal​(@Nullable
                                                       View view,
                                                       boolean consume)
        Apply horizontal window insets padding for the supplied view.
        Parameters:
        view - The view to set the insets padding.
        consume - true to consume the applied window insets.
        See Also:
        applyWindowInsets(View, boolean, boolean, boolean, boolean, boolean)
      • applyWindowInsetsVertical

        public static void applyWindowInsetsVertical​(@Nullable
                                                     View view,
                                                     boolean consume)
        Apply vertical window insets padding for the supplied view.
        Parameters:
        view - The view to set the insets padding.
        consume - true to consume the applied window insets.
        See Also:
        applyWindowInsets(View, boolean, boolean, boolean, boolean, boolean)
      • applyWindowInsetsHorizontalBottom

        public static void applyWindowInsetsHorizontalBottom​(@Nullable
                                                             View view,
                                                             boolean consume)
        Apply horizontal and bottom window insets padding for the supplied view.
        Parameters:
        view - The view to set the insets padding.
        consume - true to consume the applied window insets.
        See Also:
        applyWindowInsets(View, boolean, boolean, boolean, boolean, boolean)
      • applyWindowInsetsMargin

        public static void applyWindowInsetsMargin​(@Nullable
                                                   View view,
                                                   boolean left,
                                                   boolean top,
                                                   boolean right,
                                                   boolean bottom,
                                                   boolean consume)
        Apply window insets margin for the supplied view.
        Parameters:
        view - The view to set the insets margin.
        left - true to apply the left window inset margin.
        top - true to apply the top window inset margin.
        right - true to apply the right window inset margin.
        bottom - true to apply the bottom window inset margin.
        consume - true to consume the applied window margin.
      • requestApplyWindowInsets

        public static void requestApplyWindowInsets​(@Nullable
                                                    View view)
        Request to apply window insets for a view
        Parameters:
        view - The view to request the window insets.
      • setTextViewTextSize

        public static void setTextViewTextSize​(@Nullable
                                               RemoteViews remoteViews,
                                               @IdRes
                                               int viewId,
                                               int units,
                                               float size)
        Equivalent to calling TextView.setTextSize(int, float).
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        units - The units of size (e.g. COMPLEX_UNIT_SP).
        size - The size of the text.
      • setTextViewMaxLines

        public static void setTextViewMaxLines​(@Nullable
                                               RemoteViews remoteViews,
                                               @IdRes
                                               int viewId,
                                               int lines)
        Set the text view max lines for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        lines - The max lines to be set.
        See Also:
        RemoteViews.setInt(int, String, int)
      • setTextViewText

        public static void setTextViewText​(@Nullable
                                           RemoteViews remoteViews,
                                           @IdRes
                                           int viewId,
                                           @Nullable
                                           java.lang.CharSequence text,
                                           boolean visible)
        Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        text - The text to be set.
        visible - true to make the text view visible in case of non-empty text.
      • setTextViewTextHtml

        public static void setTextViewTextHtml​(@Nullable
                                               RemoteViews remoteViews,
                                               @IdRes
                                               int viewId,
                                               @Nullable
                                               java.lang.CharSequence text,
                                               boolean visible)
        Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        text - The text to be set.
        visible - true to make the text view visible in case of non-empty text.
        See Also:
        HtmlCompat.fromHtml(String, int), setTextViewText(RemoteViews, int, CharSequence, boolean)
      • setTextViewAllCaps

        public static void setTextViewAllCaps​(@Nullable
                                              RemoteViews remoteViews,
                                              @IdRes
                                              int viewId,
                                              boolean allCaps)
        Set the text view all caps for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        allCaps - true to set the all caps.
        See Also:
        RemoteViews.setBoolean(int, String, boolean)
      • setTextViewAllCaps

        public static void setTextViewAllCaps​(@Nullable
                                              TextView textView,
                                              boolean allCaps)
        Set the text view all caps.
        Parameters:
        textView - The text view to be used.
        allCaps - true to set the all caps.
        See Also:
        TextView.setAllCaps(boolean)
      • setAlpha

        public static void setAlpha​(@Nullable
                                    View view,
                                    @FloatRange(from=0.0,to=1.0)
                                    float alpha)
        Set the alpha for the supplied view.
        Parameters:
        view - The view to set the alpha.
        alpha - The alpha value to be set.
        See Also:
        View.setAlpha(float)
      • setAlpha

        public static void setAlpha​(@Nullable
                                    RemoteViews remoteViews,
                                    @IdRes
                                    int viewId,
                                    @IntRange(from=0L,to=255L)
                                    int alpha)
        Set the alpha for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        alpha - The alpha value to be set.
        See Also:
        RemoteViews.setInt(int, String, int)
      • setEnabled

        public static void setEnabled​(@Nullable
                                      RemoteViews remoteViews,
                                      @IdRes
                                      int viewId,
                                      boolean enabled)
        Set the enabled state for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        enabled - true to enable the view.
        See Also:
        RemoteViews.setBoolean(int, String, boolean)
      • setBackgroundColor

        public static void setBackgroundColor​(@Nullable
                                              RemoteViews remoteViews,
                                              @IdRes
                                              int viewId,
                                              @ColorInt
                                              int color)
        Set the background color for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        color - The color value to be set.
        See Also:
        RemoteViews.setInt(int, String, int)
      • setColorFilter

        public static void setColorFilter​(@Nullable
                                          RemoteViews remoteViews,
                                          @IdRes
                                          int viewId,
                                          @ColorInt
                                          int color)
        Set the color filter for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        color - The color value to be set.
        See Also:
        RemoteViews.setInt(int, String, int)
      • setTextViewLinkColor

        public static void setTextViewLinkColor​(@Nullable
                                                RemoteViews remoteViews,
                                                @IdRes
                                                int viewId,
                                                @ColorInt
                                                int color)
        Set the text view link color for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        color - The color to be set.
        See Also:
        RemoteViews.setInt(int, String, int)
      • setTextViewColors

        public static void setTextViewColors​(@Nullable
                                             RemoteViews remoteViews,
                                             @IdRes
                                             int viewId,
                                             @ColorInt
                                             int color,
                                             @ColorInt
                                             int linkColor)
        Set the text view colors for the remote views.
        Parameters:
        remoteViews - The remote views to be used.
        viewId - The id of the view to be used.
        color - The color to be set.
        linkColor - The link color to be set.
        See Also:
        RemoteViews.setTextColor(int, int), setTextViewLinkColor(RemoteViews, int, int)