Class Utils


public class Utils extends Object
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • getStringResourceId

      @StringRes public static int getStringResourceId(Context context, @NonNull AttributeSet attributeSet, @AttrRes int attributeId)
      Returns the string resource id that this attributeId is resolving to under the provided attribute set and the current theme.

      If the attribute does not point to a resource id, the method returns "0". This can happen if the attribute is set to a hardcoded string or to "@null" or the attribute has not beet set.

      context - The context.
      attributeSet - The view's attribute set.
      attributeId - The desired attribute to be retrieved.
      The attribute's resource identifier or "0" if not applicable.
    • printAttributeSet

      public static void printAttributeSet(Context context, @NonNull AttributeSet attrs)
    • getCurrentLocale

      public static Locale getCurrentLocale(Context context)
      Utility method to get the current locale as set in Android.
    • getLocalizedResources

      @NonNull public static Resources getLocalizedResources(@NonNull Context context, @NonNull Locale desiredLocale)
      Returns a Resources object, derived from the one in the provided context, which uses the desired locale.
    • getDefaultLocaleResources

      @NonNull public static Resources getDefaultLocaleResources(@NonNull Context context)
      Returns a Resources object configured for the default (non localized) resources.

      Getting a string from this object, will return the string found in the default `strings.xml` file. Note though that quantity strings will not follow any locale's plural rules.

    • equals

      public static boolean equals(Object a, Object b)
    • fromHtml

      @NonNull public static Spanned fromHtml(@NonNull String source, int flags)
      Invokes Html.fromHtml(String, int) on API 24 and newer, otherwise flags are ignored and Html.fromHtml(String) is used.
    • unescapeHTMLEntities

      public static String unescapeHTMLEntities(String string)
      Converts HTML entities to the final character similarly to what HtmlCompat.fromHTML() or the Android XML parser would do.

      The following conversions are performed:

      • "&" to "&"
      • "&lt;" to "<"
      • "&gt;" to ">"

      Only the HTML entities supported by the Android XML parser are converted.

      string - A string that may contain HTML entities.
      The finally rendered string.
    • isClassPresent

      public static boolean isClassPresent(String className)
      Checks, using reflection, if the provided class is available at runtime.
    • isAppcompatPresent

      public static boolean isAppcompatPresent()
      Checks if Androidx.appcompat classes are available at runtime.
    • isMaterialComponentsPresent

      public static boolean isMaterialComponentsPresent()
      Checks if Material Components classes are available at runtime.