Class StyleHelper


  • public class StyleHelper
    extends java.lang.Object
    Helper class for Style-related methods. If you want to implement rules that are applied to all views, that's the right place. Also contains some general measurement/general purpose helpers.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void applyCommonRules​(android.view.View targetView, java.util.Map<java.lang.String,​java.lang.String> rules)
      Apply common rules to a view.
      static int darkenColor​(int color)
      Darkens a color, using HSL
      static int dpToPixels​(android.content.res.Resources resources, java.lang.Float dp)
      Converts a density-independent pixels measurement value to pixels.
      static FlexboxLayout.LayoutParams getFlexLayoutParams​(android.content.Context context, FlexboxLayout.LayoutParams baseParams, java.util.Map<java.lang.String,​java.lang.String> rules)
      Generate FlexboxLayout.LayoutParams for the given CSS rules.
      static android.widget.FrameLayout.LayoutParams getFrameLayoutParams​(android.content.Context context, android.widget.FrameLayout.LayoutParams base, java.util.Map<java.lang.String,​java.lang.String> rules)
      Generate FrameLayout.LayoutParams for the given CSS rules.
      static PercentRelativeLayout.LayoutParams getRelativeLayoutParams​(android.content.Context context, PercentRelativeLayout.LayoutParams base, java.util.Map<java.lang.String,​java.lang.String> rules, int defaultHorizontalAlign, android.view.View relativeToView)
      Generate RelativeLayout.LayoutParams for the given CSS rules.
      static java.lang.Float optFloat​(java.lang.String stringValue)
      Returns the float value of a string.
      static java.lang.Integer optInt​(java.lang.String stringValue)
      Returns the integer value of a string.
      static int parseColor​(java.lang.String color)
      Parse a color.
      static Document parseStyle​(java.lang.String css)
      Parse raw css
      static float pixelsToDp​(android.content.res.Resources resources, java.lang.Float px)
      Converts a pixels measurement value to density-independent pixels.
      • Methods inherited from class java.lang.Object

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

      • parseStyle

        public static Document parseStyle​(@Nullable
                                          java.lang.String css)
        Parse raw css
        Throws:
        java.lang.IllegalArgumentException - Throws if the css isn't valid
      • applyCommonRules

        public static void applyCommonRules​(android.view.View targetView,
                                            java.util.Map<java.lang.String,​java.lang.String> rules)
        Apply common rules to a view.

        While this method tries to peek at the View's class to avoid code duplication (especially for the background drawable stuff), a view implementing specific rules should extends its base class and implement the Styleable interface.

        Parameters:
        targetView - View that the rules will be applied to.
        rules - CSS-like rules.
      • getFlexLayoutParams

        public static FlexboxLayout.LayoutParams getFlexLayoutParams​(android.content.Context context,
                                                                     FlexboxLayout.LayoutParams baseParams,
                                                                     java.util.Map<java.lang.String,​java.lang.String> rules)
        Generate FlexboxLayout.LayoutParams for the given CSS rules.
        Parameters:
        context - The view's context
        baseParams - Base layout parameters. Pass "null" if you want default rules to be used (WRAP_CONTENT)
        rules - CSS-like rules
        Returns:
        Layout parameters matching the CSS rules
      • getRelativeLayoutParams

        public static PercentRelativeLayout.LayoutParams getRelativeLayoutParams​(android.content.Context context,
                                                                                 PercentRelativeLayout.LayoutParams base,
                                                                                 java.util.Map<java.lang.String,​java.lang.String> rules,
                                                                                 int defaultHorizontalAlign,
                                                                                 android.view.View relativeToView)
        Generate RelativeLayout.LayoutParams for the given CSS rules.
        Parameters:
        context - The view's context
        base - Base layout parameters. Pass "null" if you want default rules to be used (MATCH_PARENT)
        rules - CSS-like rules
        relativeToView - Sets if the alignments should be relative to a view. If null, _PARENT variants of the rules will be used when applicable
        Returns:
        Layout parameters matching the CSS rules
      • getFrameLayoutParams

        public static android.widget.FrameLayout.LayoutParams getFrameLayoutParams​(android.content.Context context,
                                                                                   android.widget.FrameLayout.LayoutParams base,
                                                                                   java.util.Map<java.lang.String,​java.lang.String> rules)
        Generate FrameLayout.LayoutParams for the given CSS rules.
        Parameters:
        context - The view's context
        base - Base layout parameters. Pass "null" if you want default rules to be used (MATCH_PARENT)
        rules - CSS-like rules
        Returns:
        Layout parameters matching the CSS rules
      • dpToPixels

        public static int dpToPixels​(android.content.res.Resources resources,
                                     java.lang.Float dp)
        Converts a density-independent pixels measurement value to pixels.
        Parameters:
        resources - View's resources
        dp - Density-independent pixel value
        Returns:
        The corresponding pixel size for the current resources density
      • pixelsToDp

        public static float pixelsToDp​(android.content.res.Resources resources,
                                       java.lang.Float px)
        Converts a pixels measurement value to density-independent pixels.
        Parameters:
        resources - View's resources
        px - Pixel value
        Returns:
        The corresponding density-independent pixel size for the current resources density
      • optInt

        public static java.lang.Integer optInt​(java.lang.String stringValue)
        Returns the integer value of a string. Doesn't throw an exception if it fails.
        Parameters:
        stringValue - String to parse
        Returns:
        Integer value if possible, null otherwise
      • optFloat

        public static java.lang.Float optFloat​(java.lang.String stringValue)
        Returns the float value of a string. Doesn't throw an exception if it fails.
        Parameters:
        stringValue - String to parse
        Returns:
        Float value if possible, null otherwise
      • parseColor

        public static int parseColor​(java.lang.String color)
        Parse a color. Accepts "#rrggbb", "#aarrggbb" or "transparent". Will fallback on Color.TRANSPARENT when an error occurs.
        Parameters:
        color - Color string to parse
        Returns:
        Parsed color, or Color.TRANSPARENT
      • darkenColor

        public static int darkenColor​(int color)
        Darkens a color, using HSL
        Parameters:
        color - Color to be darkened
        Returns:
        Darkened color