Class UiUtils



  • public class UiUtils
    extends java.lang.Object
    Conversion utils for screen related items.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      static class  UiUtils.ScreenSize 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static void dismissKeyboard(Activity activity)
      Attempts to dismiss the software keyboard.
      static int dpToPixels(int sizeInDp, DisplayMetrics displayMetrics)
      Converts a size in display independent pixels to real pixels.
      static UiUtils.ScreenSize getScreenSize(Context context)
      Gets the size of the screen.
      static boolean isTablet(Context context)
      Checks if the current device is a tablet.
      static void setThemeIfAttributesAreMissing(Activity activity, int... attributes)
      Sets a Theme on an activity if one or more required attributes do not have a value.
      static void setVisibility(View view, int visibilityState)
      Sets a view's visibility to one of View#VISIBLE, View#INVISIBLE or View#GONE.
      static void sizeDialogWidthForTablets(Dialog dialog, float percentageWidth)
      Dialogs on tablets can be a bit narrow, so this method can be used to resize them to be larger.
      static int spToPixels(int sizeInSp, DisplayMetrics displayMetrics)
      Converts a size in scaled pixels to real pixels.
      static int themeAttributeToColor(int themeAttributeId, Context context, int fallbackColorId)
      Converts a attribute id in the current theme to the corresponding resolved color value.
      static int themeAttributeToPixels(int themeAttributeId, Context context, int fallbackUnitType, float fallbackUnitValue)
      Converts a themed attribute id to the corresponding number of real pixels.
      • Methods inherited from class java.lang.Object

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

      • dpToPixels

        public static int dpToPixels(int sizeInDp,
                                     DisplayMetrics displayMetrics)
        Converts a size in display independent pixels to real pixels.

        The number of pixels is calculated as a float internally and rounded before it is returned. This method should not ne used to calculate text sizes.

        Parameters:
        sizeInDp - The size in DP
        displayMetrics - The display metrics
        Returns:
        The number of real pixels for the specified number of DP
      • spToPixels

        public static int spToPixels(int sizeInSp,
                                     DisplayMetrics displayMetrics)
        Converts a size in scaled pixels to real pixels.

        The number of pixels is calculated as a float internally and rounded before it is returned. This method should be used for calculating text sizes.

        Parameters:
        sizeInSp - The size in SP
        displayMetrics - The display metrics
        Returns:
        The number of real pixels for the specified number of SP
      • themeAttributeToPixels

        public static int themeAttributeToPixels(int themeAttributeId,
                                                 Context context,
                                                 int fallbackUnitType,
                                                 float fallbackUnitValue)
        Converts a themed attribute id to the corresponding number of real pixels.

        This method will a dimension that has been specified in a style XML file like this:
        <attr name="RateMyAppPaddingBottom" format="reference|dimension" />

        Parameters:
        themeAttributeId - The attr id
        context - Must be a UI context, must not be an application context.
        fallbackUnitType - Only used when there was an issue looking up the unit by ID. Usually one of DIP or SP
        fallbackUnitValue - Only used when there was an issue looking up the unit by ID How many units of the type of fallbackUnitType to return if there was an error looking up the unit by themeAttributeId
        Returns:
        The size of this dimension in pixels
        Throws:
        java.lang.IllegalArgumentException - if a non-UI context is supplied
      • themeAttributeToColor

        public static int themeAttributeToColor(int themeAttributeId,
                                                Context context,
                                                int fallbackColorId)
        Converts a attribute id in the current theme to the corresponding resolved color value.

        This method will resolve a color that has been specified in a style XML file like this:
        <item name="colorPrimary">@color/my_primary_color</item>
        This will return the value of @color/my_primary_color.

        Parameters:
        themeAttributeId - The color attr id
        context - Must be a UI context, must not be an application context. It must also be using the theme the attribute is declared in.
        fallbackColorId - Only used when there was an issue looking up the attribute color ID, as a second choice. This should be a color reference directly, as in R.color rather than R.attr
        Returns:
        The resolved color value of themeAttributeId, if found, or fallbackColorId, if not. If invalid IDs, or a null context is supplied, then Color#BLACK will be returned
      • getScreenSize

        public static UiUtils.ScreenSize getScreenSize(Context context)
        Gets the size of the screen.

        The screen size is not the same as the various dpi levels. A screen can have the same dpi level like xhdpi but the screen size tells us whether this is a phone or tablet device.

        Parameters:
        context - The context
        Returns:
        The actual screen size or UiUtils.ScreenSize.UNDEFINED or UiUtils.ScreenSize.UNKNOWN
      • isTablet

        public static boolean isTablet(Context context)
        Checks if the current device is a tablet.

        Tablets are specifically identified by having screen sizes of either LARGE or X-LARGE. Note that this is not xhdpi / hdpi but the value that is determined by #getScreenSize(android.content.Context)

        Parameters:
        context - The context
        Returns:
        true if the device is a tablet, false if it is not or if we couldn't reliably determine
      • sizeDialogWidthForTablets

        public static void sizeDialogWidthForTablets(Dialog dialog,
                                                     float percentageWidth)
        Dialogs on tablets can be a bit narrow, so this method can be used to resize them to be larger.
        Parameters:
        dialog - The Dialog to resize
        percentageWidth - The width that the dialog should be resized to as a percentage of the total width of the screen.
      • setVisibility

        public static void setVisibility(View view,
                                         int visibilityState)
        Sets a view's visibility to one of View#VISIBLE, View#INVISIBLE or View#GONE.

        If the supplied view is null then no action will be attempted.

        Parameters:
        view - The view to change the visibility of
        visibilityState - The visibility to set
      • dismissKeyboard

        public static void dismissKeyboard(Activity activity)
        Attempts to dismiss the software keyboard.
        Parameters:
        activity - The activity used to obtain an android.view.inputmethod.InputMethodManager and the current focus' window token
      • setThemeIfAttributesAreMissing

        public static void setThemeIfAttributesAreMissing(Activity activity,
                                                          int... attributes)
        Sets a Theme on an activity if one or more required attributes do not have a value.

        This method needs to be called before you set Activity#setContentView(int) on the activity.

        Parameters:
        activity - The activity which is dependent on certain attributes in order to display properly
        attributes - The attributes which must have a value for the activity to display properly