Class DynamicLayoutUtils

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

public class DynamicLayoutUtils extends Object
Helper class to perform layout operations like detecting the column count at runtime.
  • Field Details

    • SPAN_DEFAULT

      public static final int SPAN_DEFAULT
      Default column count for the collection views.
      See Also:
  • Constructor Details

    • DynamicLayoutUtils

      public DynamicLayoutUtils()
  • Method Details

    • getLayoutColumns

      public static int getLayoutColumns(@NonNull Context context, int defaultCount, int maxCount, boolean compact)
      Get the column count according to the current configuration.

      It will also consider multi-window mode on API 24 and above.

      It is not recommended to do this calculation at runtime. So, please define all the span counts in xml.

      Parameters:
      context - The context to get configuration.
      defaultCount - The default column count.
      maxCount - The maximum column count up to which we can scale.
      compact - true if the layout is compact and disable the auto increase of columns in multi-window mode.
      Returns:
      The column count according to the current device configurations.
    • isInMultiWindowMode

      public static boolean isInMultiWindowMode(@NonNull Context context)
      Checks whether the app is in multi-window mode.
      Parameters:
      context - The context to get configuration.
      Returns:
      true if the app is in multi-window mode.
    • getScreenSizeCategory

      public static int getScreenSizeCategory(@NonNull Context context)
      Get the screen size category for the supplied context.
      Parameters:
      context - The context to get configuration.
      Returns:
      The screen size category for the supplied context.
    • getGridCount

      public static int getGridCount(@NonNull Context context)
      Get the grid count for the supplied context.
      Parameters:
      context - The context to get the span count.
      Returns:
      The grid count for the supplied context with default count 1 and max count 2.
    • getGridCountNoDrawer

      public static int getGridCountNoDrawer(@NonNull Context context)
      Get the grid count for the supplied context suitable for no drawer activity.
      Parameters:
      context - The context to get the span count.
      Returns:
      The grid count for the supplied context with default count 1 and max count 3.
    • getGridCountCompact

      public static int getGridCountCompact(@NonNull Context context)
      Get the grid count for the supplied context suitable for a compact layout.
      Parameters:
      context - The context to get the span count.
      Returns:
      The grid count for the supplied context with default count 1 or 2 and max count 3.
    • getGridCountCompactDialog

      public static int getGridCountCompactDialog(@NonNull Context context)
      Get the grid count for the supplied context suitable for the dialog.
      Parameters:
      context - The context to get the span count.
      Returns:
      The grid count for the supplied context with default count 1 or 2 and max count 3.
    • getLinearLayoutManager

      @NonNull public static LinearLayoutManager getLinearLayoutManager(@NonNull Context context, int orientation)
      Returns the LinearLayoutManager object for the given context.
      Parameters:
      context - The context to instantiate the layout manager.
      orientation - The orientation of the layout manager. LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL
      Returns:
      The LinearLayoutManager object for the given context.
    • getGridLayoutManager

      @NonNull public static GridLayoutManager getGridLayoutManager(@NonNull Context context, int count, int orientation)
      Returns the GridLayoutManager object for the given context.
      Parameters:
      context - The context to instantiate the layout manager.
      count - The column count for the grid layout.
      orientation - The orientation of the layout manager. LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL
      Returns:
      The GridLayoutManager object for the given context.
    • getStaggeredGridLayoutManager

      @NonNull public static StaggeredGridLayoutManager getStaggeredGridLayoutManager(int count, int orientation)
      Returns the StaggeredGridLayoutManager object for the given context.
      Parameters:
      count - The no. of rows or columns count according to the orientation.
      orientation - The orientation of the layout manager. StaggeredGridLayoutManager.VERTICAL or StaggeredGridLayoutManager.HORIZONTAL
      Returns:
      The StaggeredGridLayoutManager object for the given context.
    • getFlexboxLayoutManager

      @NonNull public static com.google.android.flexbox.FlexboxLayoutManager getFlexboxLayoutManager(@NonNull Context context, int flexDirection, int justifyContent, int alignItems)
      Returns the FlexboxLayoutManager object for the given context.
      Parameters:
      context - The context to instantiate the layout manager.
      flexDirection - The flex direction attribute to the flex container.
      justifyContent - The justify content attribute to the flex container.
      alignItems - The align items attribute to the flex container.
      Returns:
      The FlexboxLayoutManager object for the given context.
    • getFlexboxLayoutManager

      @NonNull public static com.google.android.flexbox.FlexboxLayoutManager getFlexboxLayoutManager(@NonNull Context context, int flexDirection)
      Returns the FlexboxLayoutManager object for the given context.
      Parameters:
      context - The context to instantiate the layout manager.
      flexDirection - The flex direction attribute to the flex container.
      Returns:
      The FlexboxLayoutManager object for the given context.
    • setFullSpanForType

      public static void setFullSpanForType(@Nullable RecyclerView recyclerView, @NonNull Integer[] itemTypes, int spanCount)
      Sets full span for the item view types in case of a GridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      recyclerView - The recycler view to set the span size.
      itemTypes - The item types supported by the recycler view.
      spanCount - The maximum span supported by the layout manager.
      See Also:
    • setFullSpanForType

      public static void setFullSpanForType(@Nullable RecyclerView recyclerView)
      Sets full span for the header and empty view in case of a GridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      recyclerView - The recycler view to set the span size.
      See Also:
    • setFullSpanForPosition

      public static void setFullSpanForPosition(@Nullable RecyclerView recyclerView, @NonNull Integer[] positions, int spanCount)
      Sets full span for the positions in case of a GridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      recyclerView - The recycler view to set the span size.
      positions - The positions supported by the recycler view.
      spanCount - The maximum span supported by the layout manager.
      See Also:
    • setFullSpanForPosition

      public static void setFullSpanForPosition(@Nullable RecyclerView recyclerView, @NonNull Integer[] positions)
      Sets full span for the positions in case of a GridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      recyclerView - The recycler view to set the span size.
      positions - The positions supported by the recycler view.
      See Also:
    • setFullSpanForPosition

      public static void setFullSpanForPosition(@Nullable RecyclerView recyclerView)
      Sets full span for the single last position in case of a GridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      recyclerView - The recycler view to set the span size.
      See Also:
    • setFullSpanForView

      public static void setFullSpanForView(@Nullable View view)
      Sets full span for the view in case of a StaggeredGridLayoutManager. This method must be called after setting an adapter for the recycler view.
      Parameters:
      view - The recycler view to set the span size.
      See Also: