Class DynamicViewUtils


  • public class DynamicViewUtils
    extends java.lang.Object
    Helper class to perform View operations.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void addView​(android.view.ViewGroup viewGroup, int layoutRes, boolean removePrevious)
      Add a view to the view group.
      static void addView​(android.view.ViewGroup viewGroup, android.view.View view, boolean removePrevious)
      Add a view to the view group.
      static void applyWindowInsets​(android.view.View view, boolean left, boolean top, boolean right, boolean bottom, boolean consume)
      Apply window insets padding for the supplied view.
      static void applyWindowInsetsBottom​(android.view.View view)
      Apply bottom window insets padding for the supplied view.
      static void applyWindowInsetsBottom​(android.view.View view, boolean consume)
      Apply bottom window insets padding for the supplied view.
      static void applyWindowInsetsHorizontal​(android.view.View view, boolean consume)
      Apply horizontal window insets padding for the supplied view.
      static void applyWindowInsetsHorizontalBottom​(android.view.View view)
      Apply horizontal and bottom window insets padding for the supplied view.
      static void applyWindowInsetsHorizontalBottom​(android.view.View view, boolean consume)
      Apply horizontal and bottom window insets padding for the supplied view.
      static void applyWindowInsetsMargin​(android.view.View view, boolean left, boolean top, boolean right, boolean bottom, boolean consume)
      Apply window insets margin for the supplied view.
      static void applyWindowInsetsMarginBottom​(android.view.View view)
      Apply bottom window insets margin for the supplied view.
      static void applyWindowInsetsMarginHorizontal​(android.view.View view, boolean consume)
      Apply horizontal window insets margin for the supplied view.
      static void applyWindowInsetsMarginHorizontalBottom​(android.view.View view)
      Apply horizontal and bottom window insets margin for the supplied view.
      static void applyWindowInsetsMarginVertical​(android.view.View view, boolean consume)
      Apply vertical window insets margin for the supplied view.
      static void applyWindowInsetsVertical​(android.view.View view, boolean consume)
      Apply vertical window insets padding for the supplied view.
      static boolean isEdgeToEdge​(android.view.View view)
      Deprecated.
      Try to use WindowInsetsControllerCompat if possible.
      static boolean isLayoutRtl​(android.view.View view)
      Checks whether the layout direction is RTL (right-to-left).
      static <T extends android.view.View>
      boolean
      isRootLayout​(T view)
      Checks if the supplied view is the only root layout in the view heirarchy.
      static void manageScrollIndicators​(android.view.View view, android.view.View upIndicator, android.view.View downIndicator)
      Manage scroll indicators for a view according to its current state.
      static void requestApplyWindowInsets​(android.view.View view)
      Request to apply window insets for a view
      static void setAlpha​(android.view.View view, float alpha)
      Set the alpha for the supplied view.
      static void setAlpha​(android.widget.RemoteViews remoteViews, int viewId, int alpha)
      Set the alpha for the remote views.
      static void setBackgroundColor​(android.widget.RemoteViews remoteViews, int viewId, int color)
      Set the background color for the remote views.
      static void setColorFilter​(android.widget.RemoteViews remoteViews, int viewId, int color)
      Set the color filter for the remote views.
      static void setEdgeToEdge​(android.view.View view, boolean edgeToEdge)
      Deprecated.
      Use DynamicWindowUtils.setEdgeToEdge(Window, boolean) to support latest API levels.
      static void setEnabled​(android.widget.RemoteViews remoteViews, int viewId, boolean enabled)
      Set the enabled state for the remote views.
      static void setLightNavigationBar​(android.view.View view, boolean light)
      Set light navigation bar if we are using light primary color on API 26 and above.
      static void setLightStatusBar​(android.view.View view, boolean light)
      Set light status bar if we are using light primary color on API 23 and above.
      static void setTextSwitcherText​(android.widget.TextSwitcher view, java.lang.CharSequence text)
      Set the text switcher text and animate only if there is a text change.
      static void setTextViewAllCaps​(android.widget.RemoteViews remoteViews, int viewId, boolean allCaps)
      Set the text view all caps for the remote views.
      static void setTextViewAllCaps​(android.widget.TextView textView, boolean allCaps)
      Set the text view all caps.
      static void setTextViewColors​(android.widget.RemoteViews remoteViews, int viewId, int color, int linkColor)
      Set the text view colors for the remote views.
      static void setTextViewLinkColor​(android.widget.RemoteViews remoteViews, int viewId, int color)
      Set the text view link color for the remote views.
      static void setTextViewMaxLines​(android.widget.RemoteViews remoteViews, int viewId, int lines)
      Set the text view max lines for the remote views.
      static void setTextViewText​(android.widget.RemoteViews remoteViews, int viewId, java.lang.CharSequence text)
      Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
      static void setTextViewText​(android.widget.RemoteViews remoteViews, int viewId, java.lang.CharSequence text, boolean visible)
      Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
      static void setTextViewTextHtml​(android.widget.RemoteViews remoteViews, int viewId, java.lang.CharSequence text)
      Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
      static void setTextViewTextHtml​(android.widget.RemoteViews remoteViews, int viewId, java.lang.CharSequence text, boolean visible)
      Equivalent to calling RemoteViews.setTextViewText(int, CharSequence) and hide the view if the text is empty.
      static void setTextViewTextSize​(android.widget.RemoteViews remoteViews, int viewId, int units, float size)
      Equivalent to calling TextView.setTextSize(int, float).
      • Methods inherited from class java.lang.Object

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

      • DynamicViewUtils

        public DynamicViewUtils()
    • Method Detail

      • isRootLayout

        public static <T extends android.view.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
                                         android.view.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
                                           android.view.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
                                             android.view.View view,
                                             boolean light)
        Set light status bar if we are using light primary color 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
                                                 android.view.View view,
                                                 boolean light)
        Set light navigation bar if we are using light primary color 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
                                   android.view.ViewGroup viewGroup,
                                   @Nullable
                                   android.view.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
                                   android.view.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
                                                  android.view.View view,
                                                  @Nullable
                                                  android.view.View upIndicator,
                                                  @Nullable
                                                  android.view.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
                                          android.view.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
                                             android.view.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
                                                       android.view.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
                                                     android.view.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)
      • applyWindowInsetsBottom

        public static void applyWindowInsetsBottom​(@Nullable
                                                   android.view.View view,
                                                   boolean consume)
        Apply 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)
      • applyWindowInsetsHorizontalBottom

        public static void applyWindowInsetsHorizontalBottom​(@Nullable
                                                             android.view.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
                                                   android.view.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.
      • applyWindowInsetsMarginHorizontal

        public static void applyWindowInsetsMarginHorizontal​(@Nullable
                                                             android.view.View view,
                                                             boolean consume)
        Apply horizontal window insets margin for the supplied view.
        Parameters:
        view - The view to set the insets margin.
        consume - true to consume the applied window insets.
        See Also:
        applyWindowInsetsMargin(View, boolean, boolean, boolean, boolean, boolean)
      • applyWindowInsetsMarginVertical

        public static void applyWindowInsetsMarginVertical​(@Nullable
                                                           android.view.View view,
                                                           boolean consume)
        Apply vertical window insets margin for the supplied view.
        Parameters:
        view - The view to set the insets margin.
        consume - true to consume the applied window insets.
        See Also:
        applyWindowInsetsMargin(View, boolean, boolean, boolean, boolean, boolean)
      • requestApplyWindowInsets

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

        public static void setTextSwitcherText​(@Nullable
                                               android.widget.TextSwitcher view,
                                               @Nullable
                                               java.lang.CharSequence text)
        Set the text switcher text and animate only if there is a text change.
        Parameters:
        view - The view switcher to be used.
        text - The text to be set.
        See Also:
        TextSwitcher.setCurrentText(CharSequence), TextSwitcher.setText(CharSequence)
      • setTextViewTextSize

        public static void setTextViewTextSize​(@Nullable
                                               android.widget.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
                                               android.widget.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
                                           android.widget.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.
      • setTextViewText

        public static void setTextViewText​(@Nullable
                                           android.widget.RemoteViews remoteViews,
                                           @IdRes
                                           int viewId,
                                           @Nullable
                                           java.lang.CharSequence text)
        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.
        See Also:
        setTextViewText(RemoteViews, int, CharSequence, boolean)
      • setTextViewTextHtml

        public static void setTextViewTextHtml​(@Nullable
                                               android.widget.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)
      • setTextViewTextHtml

        public static void setTextViewTextHtml​(@Nullable
                                               android.widget.RemoteViews remoteViews,
                                               @IdRes
                                               int viewId,
                                               @Nullable
                                               java.lang.CharSequence text)
        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.
        See Also:
        setTextViewTextHtml(RemoteViews, int, CharSequence, boolean)
      • setTextViewAllCaps

        public static void setTextViewAllCaps​(@Nullable
                                              android.widget.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
                                              android.widget.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
                                    android.view.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
                                    android.widget.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
                                      android.widget.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
                                              android.widget.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
                                          android.widget.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
                                                android.widget.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
                                             android.widget.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)