Class DynamicResourceUtils

java.lang.Object
com.pranavpandey.android.dynamic.support.util.DynamicResourceUtils

public class DynamicResourceUtils extends Object
Helper class to perform resource operations.

ContextCompat and AppCompatResources are used to provide backward compatibility.

  • Field Details

    • ADS_DEFAULT_RESOURCE_ID

      public static final int ADS_DEFAULT_RESOURCE_ID
      Resource id constant for the no resource.
      See Also:
    • ADS_DEFAULT_RESOURCE_VALUE

      public static final int ADS_DEFAULT_RESOURCE_VALUE
      Resource value constant for the no value.
      See Also:
  • Constructor Details

    • DynamicResourceUtils

      public DynamicResourceUtils()
  • Method Details

    • getAttributeResId

      public static int getAttributeResId(@NonNull Context context, @AttrRes int attr)
      Extract the supplied attribute value resource id from the theme.
      Parameters:
      context - The context to be used.
      attr - The attribute whose value resource id to be extracted.
      Returns:
      The value resource id of the supplied attribute.
    • resolveInteger

      public static int resolveInteger(@NonNull Context context, @StyleRes int theme, @AttrRes int attr, int defaultValue)
      Extract the supplied integer attribute value from the theme.
      Parameters:
      context - The context to be used.
      theme - The theme to get the styled attributes.
      attr - The integer attribute whose value should be extracted.
      defaultValue - The value to return if the attribute is not defined or not a resource.
      Returns:
      The value of the supplied attribute.
    • resolveColor

      @ColorInt public static int resolveColor(@NonNull Context context, @StyleRes int theme, @AttrRes int attr, int defaultValue)
      Extract the supplied color attribute value from the theme.
      Parameters:
      theme - The theme to get the styled attributes.
      context - The context to be used.
      attr - The color attribute whose value to be extracted.
      defaultValue - The value to return if the attribute is not defined or not a resource.
      Returns:
      The value of the supplied attribute.
    • resolveDrawable

      @Nullable public static Drawable resolveDrawable(@NonNull Context context, @StyleRes int theme, @AttrRes int attr)
      Extract the supplied drawable attribute value from the theme.
      Parameters:
      theme - The theme to get the styled attributes.
      context - The context to be used.
      attr - The drawable attribute whose value to be extracted.
      Returns:
      The value of the supplied attribute.
    • resolveDimension

      public static float resolveDimension(@NonNull Context context, @StyleRes int theme, @AttrRes int attr, float defaultValue)
      Extract the supplied dimension attribute value from the theme.
      Parameters:
      context - The context to be used.
      theme - The theme to get the styled attributes.
      attr - The dimension attribute whose value should be extracted.
      defaultValue - The value to return if the attribute is not defined or not a resource.
      Returns:
      The value of the supplied attribute.
    • resolveDimensionPixelOffset

      public static int resolveDimensionPixelOffset(@NonNull Context context, @StyleRes int theme, @AttrRes int attr, int defaultValue)
      Extract the supplied dimension attribute value from the theme.

      The extracted value will be converted into the integer pixels.

      Parameters:
      context - The context to be used.
      theme - The theme to get the styled attributes.
      attr - The dimension attribute whose value to be extracted.
      defaultValue - The value to return if the attribute is not defined or not a resource.
      Returns:
      The value of the supplied attribute.
    • getDrawable

      @Nullable public static Drawable getDrawable(@Nullable Context context, @DrawableRes int drawableRes)
      Get the drawable from the supplied resource.
      Parameters:
      context - The context to be used.
      drawableRes - The drawable resource to get the drawable.
      Returns:
      The drawable retrieved from the resource.
    • getBitmapFromVectorDrawable

      @NonNull public static Bitmap getBitmapFromVectorDrawable(@NonNull Drawable drawable)
      Get the bitmap from the vector drawable.
      Parameters:
      drawable - The instance of vector drawable to be converted into bitmap.
      Returns:
      The bitmap converted from the vector drawable.
    • getBitmap

      @Nullable public static Bitmap getBitmap(@Nullable Drawable drawable)
      Get the bitmap drawable from the drawable.
      Parameters:
      drawable - The drawable to be converted into bitmap drawable.
      Returns:
      The bitmap drawable converted from the drawable.
    • colorizeDrawable

      @Nullable public static Drawable colorizeDrawable(@Nullable Drawable drawable, @NonNull ColorFilter colorFilter)
      Colorize and return the mutated drawable so that, all other references do not change.
      Parameters:
      drawable - The drawable to be colorized.
      colorFilter - The color filter to be applied on the drawable.
      Returns:
      The colorized drawable.
    • colorizeDrawable

      @Nullable public static Drawable colorizeDrawable(@Nullable Drawable drawable, @ColorInt int color, @Nullable PorterDuff.Mode mode)
      Colorize and return the mutated drawable so that, all other references do not change.
      Parameters:
      drawable - The drawable to be colorized.
      color - The color to colorize the drawable.
      mode - The porter duff mode.
      Returns:
      The colorized drawable.
    • colorizeDrawableRes

      @Nullable public static Drawable colorizeDrawableRes(@NonNull Context context, @DrawableRes int drawableRes, @NonNull ColorFilter colorFilter)
      Colorize and return the mutated drawable so that, all other references do not change.
      Parameters:
      context - The context to retrieve drawable resource.
      drawableRes - The drawable resource to be colorized.
      colorFilter - The color filter to be applied on the drawable.
      Returns:
      The colorized drawable.
    • colorizeDrawableRes

      @Nullable public static Drawable colorizeDrawableRes(@NonNull Context context, @DrawableRes int drawableRes, @ColorInt int color, @Nullable PorterDuff.Mode mode)
      Colorize and return the mutated drawable so that, all other references do not change.
      Parameters:
      context - The context to retrieve drawable resource.
      drawableRes - The drawable resource to be colorized.
      color - The color to colorize the drawable.
      mode - The porter duff mode.
      Returns:
      The colorized drawable.
    • colorizeDrawableRes

      @Nullable public static Drawable colorizeDrawableRes(@NonNull Context context, @DrawableRes int drawableId, @ColorInt int color)
      Colorize and return the mutated drawable so that, all other references do not change.
      Parameters:
      context - The context to retrieve drawable resource.
      drawableId - Id of the drawable to be colorized.
      color - The color to colorize the drawable.
      Returns:
      The colorized drawable.
    • highlightQueryTextColor

      public static void highlightQueryTextColor(@Nullable String query, @Nullable TextView textView, @ColorInt int color)
      Highlight the query text within a text view. Suitable for notifying user about the searched query found in the adapter. TextView should not be empty. Please set your default text first then, highlight the query text by using this method.
      Parameters:
      query - The string to be highlighted.
      textView - The text view to set the highlighted text.
      color - The color of the highlighted text.
      See Also:
    • highlightQueryTextColorRes

      public static void highlightQueryTextColorRes(@NonNull String query, @NonNull TextView textView, @ColorRes int colorRes)
      Highlight the query text within a text view. Suitable for notifying user about the searched query found in the adapter. TextView should not be empty. Please set your default text first then, highlight the query text by using this method.
      Parameters:
      query - The string to be highlighted.
      textView - The text view to set the highlighted text.
      colorRes - The color resource of the highlighted text.
      See Also:
    • convertColorStateListWithNormal

      @NonNull public static ColorStateList convertColorStateListWithNormal(@NonNull ColorStateList colorStateList, @ColorInt int normalColor, @ColorInt int tintColor)
      Create a new color state list from the supplied one by changing its normal and tint colors.
      Parameters:
      colorStateList - The state list drawable to be converted.
      normalColor - The normal color to be applied.
      tintColor - The color to be applied.
      Returns:
      The new color state list with the applied color.
    • convertColorStateListWithNormal

      @NonNull public static ColorStateList convertColorStateListWithNormal(@NonNull ColorStateList colorStateList, @ColorInt int normalColor, @ColorInt int tintColor, @ColorInt int contrastWith)
      Create a new color state list from the supplied one by changing its normal and tint colors.
      Parameters:
      colorStateList - The state list drawable to be converted.
      normalColor - The normal color to be applied.
      tintColor - The color to be applied.
      contrastWith - The contrast color to make sure that it will always be visible on this background.
      Returns:
      The new color state list with the applied color.
    • convertColorStateList

      @NonNull public static ColorStateList convertColorStateList(@NonNull ColorStateList colorStateList, @ColorInt int color)
      Create a new color state list from the supplied one by changing its tint color.
      Parameters:
      colorStateList - The state list drawable to be converted.
      color - The color to be applied.
      Returns:
      The new color state list with the applied color.
    • convertColorStateList

      @NonNull public static ColorStateList convertColorStateList(@NonNull ColorStateList colorStateList, @ColorInt int color, @ColorInt int contrastWith)
      Create a new color state list from the supplied one by changing its tint color.
      Parameters:
      colorStateList - The state list drawable to be converted.
      color - The color to be applied.
      contrastWith - The contrast color to make sure that it will always be visible on this background.
      Returns:
      The new color state list with the applied color.
    • getColorStateList

      @NonNull public static ColorStateList getColorStateList(@ColorInt int color)
      Create a new color state list from the supplied tint color.

      Tint color will be applied on all the states.

      Parameters:
      color - The tint color to be applied.
      Returns:
      The color state list with the applied tint color.
    • getColorStateList

      @NonNull public static ColorStateList getColorStateList(@ColorInt int disabled, @ColorInt int normal, @ColorInt int pressed, @ColorInt int color, boolean checkable)
      Create a new color state list from the supplied disabled, normal and tint colors.

      Tint color will be applied on the states like checked, enabled, etc.

      Parameters:
      disabled - The color for the disabled state.
      normal - The color for the normal state.
      pressed - The color for the pressed state.
      color - The tint color to be applied.
      checkable - true if the view is checkable.
      Returns:
      The color state list with the applied normal and tint colors.
    • getColorStateList

      @NonNull public static ColorStateList getColorStateList(@ColorInt int disabled, @ColorInt int normal, @ColorInt int color, boolean checkable)
      Create a new color state list from the supplied normal and tint colors.

      Tint color will be applied on the states like checked, enabled, etc.

      Parameters:
      disabled - The color for the disabled state.
      normal - The color for the normal state.
      color - The tint color to be applied.
      checkable - true if the view is checkable.
      Returns:
      The color state list with the applied normal and tint colors.
    • getColorStateList

      @NonNull public static ColorStateList getColorStateList(@ColorInt int normal, @ColorInt int color, boolean checkable)
      Create a new color state list from the supplied normal and tint colors.

      Tint color will be applied on the states like checked, enabled, etc.

      Parameters:
      normal - The normal color to be applied.
      color - The tint color to be applied.
      checkable - true if the view is checkable.
      Returns:
      The color state list with the applied normal and tint colors.
    • getStateListDrawable

      @NonNull public static StateListDrawable getStateListDrawable(@ColorInt int disabled, @ColorInt int normal, @ColorInt int pressed, @ColorInt int color, boolean checkable)
      Create a new state list drawable from the supplied disabled, normal and tint colors.

      Tint color will be applied on the states like checked, enabled, etc.

      Parameters:
      disabled - The color for the disabled state.
      normal - The color for the normal state.
      pressed - The color for the pressed state.
      color - The tint color to be applied.
      checkable - true if the view is checkable.
      Returns:
      The state list drawable with the applied normal and tint colors.
    • getStateListDrawable

      @NonNull public static StateListDrawable getStateListDrawable(@ColorInt int normal, @ColorInt int pressed, @ColorInt int color, boolean checkable)
      Create a new state list drawable from the supplied disabled, normal and tint colors.

      Tint color will be applied on the states like checked, enabled, etc.

      Parameters:
      normal - The color for the normal state.
      pressed - The color for the pressed state.
      color - The tint color to be applied.
      checkable - true if the view is checkable.
      Returns:
      The state list drawable with the applied normal and tint colors.
    • getResourceId

      public static int getResourceId(@NonNull Context context, @AttrRes int attrRes)
      Get the value resource id of a given attribute.
      Parameters:
      context - The context to be used.
      attrRes - The resource id of the attribute.
      Returns:
      The value resource id of the supplied attribute.
    • getResourceIdFromAttributes

      public static int getResourceIdFromAttributes(@NonNull Context context, @NonNull AttributeSet attrs, @AttrRes int attrRes)
      Get the value resource id of a given attribute.
      Parameters:
      context - The context to be used.
      attrs - The supplied attribute set to load the values.
      attrRes - The resource id of the attribute.
      Returns:
      The value resource id of the supplied attribute.
    • convertToDrawableResArray

      @Nullable public static int[] convertToDrawableResArray(@NonNull Context context, @ArrayRes int arrayRes)
      Get drawable resource array from its resource id.
      Parameters:
      context - The context to be used.
      arrayRes - The resource id of the drawable array.
      Returns:
      The drawable resource array from its resource id.
    • convertToDrawableArray

      @Nullable public static Drawable[] convertToDrawableArray(@NonNull Context context, @ArrayRes int arrayRes)
      Get drawable array from ts resource id.
      Parameters:
      context - The context to be used.
      arrayRes - The resource id of the drawable array.
      Returns:
      The drawable array from its resource id.
    • convertToColorArray

      @Nullable public static Integer[] convertToColorArray(@NonNull Context context, @ArrayRes int arrayRes)
      Get color array from its resource id.
      Parameters:
      context - The context to be used.
      arrayRes - The resource id of the color array.
      Returns:
      The color array from its resource id.
    • getWindowDialogAnimation

      @StyleRes public static int getWindowDialogAnimation()
      Returns the animation for the dialog window according to the dynamic theme style.
      Returns:
      The animation for the dialog window according to the dynamic theme style.
    • getWindowPopupAnimation

      @StyleRes public static int getWindowPopupAnimation()
      Returns the animation for the popup window according to the dynamic theme style.
      Returns:
      The animation for the popup window according to the dynamic theme style.