Class StyleHelper
- java.lang.Object
-
- com.batch.android.messaging.view.helper.StyleHelper
-
public class StyleHelper extends java.lang.ObjectHelper 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 voidapplyCommonRules(android.view.View targetView, java.util.Map<java.lang.String,java.lang.String> rules)Apply common rules to a view.static intdarkenColor(int color)Darkens a color, using HSLstatic intdpToPixels(android.content.res.Resources resources, java.lang.Float dp)Converts a density-independent pixels measurement value to pixels.static FlexboxLayout.LayoutParamsgetFlexLayoutParams(android.content.Context context, FlexboxLayout.LayoutParams baseParams, java.util.Map<java.lang.String,java.lang.String> rules)GenerateFlexboxLayout.LayoutParamsfor the given CSS rules.static android.widget.FrameLayout.LayoutParamsgetFrameLayoutParams(android.content.Context context, android.widget.FrameLayout.LayoutParams base, java.util.Map<java.lang.String,java.lang.String> rules)GenerateFrameLayout.LayoutParamsfor the given CSS rules.static PercentRelativeLayout.LayoutParamsgetRelativeLayoutParams(android.content.Context context, PercentRelativeLayout.LayoutParams base, java.util.Map<java.lang.String,java.lang.String> rules, int defaultHorizontalAlign, android.view.View relativeToView)GenerateRelativeLayout.LayoutParamsfor the given CSS rules.static java.lang.FloatoptFloat(java.lang.String stringValue)Returns the float value of a string.static java.lang.IntegeroptInt(java.lang.String stringValue)Returns the integer value of a string.static intparseColor(java.lang.String color)Parse a color.static DocumentparseStyle(java.lang.String css)Parse raw cssstatic floatpixelsToDp(android.content.res.Resources resources, java.lang.Float px)Converts a pixels measurement value to density-independent pixels.
-
-
-
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
Styleableinterface.- 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)
GenerateFlexboxLayout.LayoutParamsfor the given CSS rules.- Parameters:
context- The view's contextbaseParams- 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)
GenerateRelativeLayout.LayoutParamsfor the given CSS rules.- Parameters:
context- The view's contextbase- Base layout parameters. Pass "null" if you want default rules to be used (MATCH_PARENT)rules- CSS-like rulesrelativeToView- 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)GenerateFrameLayout.LayoutParamsfor the given CSS rules.- Parameters:
context- The view's contextbase- 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 resourcesdp- 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 resourcespx- 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 onColor.TRANSPARENTwhen 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
-
-