

To start, we’ll put 10 square views into a Flow like the one in the code above. It’s simpler than setting up a RecyclerView and, for small lists of items, you won’t sacrifice the performance benefits of RecyclerView.Ī grid setup is going to work best for items that are the same size, otherwise we’ll end up with a lot of empty space.It keeps the layout flat, which is more efficient during layout calculation than nesting with GridLayout or RecyclerView.Of course, we have GridLayout and RecyclerView with GridLayoutManager, but Flow is a good alternative for a couple of reasons:

The gridĪ pretty common ask of Android developers is to create a grid of items. While there are many combinations of Flow attributes that result in a variety of layouts, I’ll focus on two use-cases that I think will be the most common usage of ConstraintLayout Flow: grid and flexbox-style layouts. If you’ve used Group or Barrier before, you’ll be familiar with this it works exactly the same as it does in those virtual helper objects. Notice the attribute constraint_referenced_ids – this attribute defines the Views that will be managed by this constraint helper. įlow is used within a parent ConstraintLayout and is able to manage any View with a defined id. However, instead of using an actual ViewGroup to manage the contained items, ConstraintLayout Flow uses a virtual helper object, so your layout maintains its flat view hierarchy. This is similar to Google’s FlexboxLayout, which is an Android implementation of the idea of the flexible box layout from CSS.

With ConstraintLayout Flow, this changes.ĬonstraintLayout Flow allows a long chain of items to wrap onto multiple rows or columns. What if you have too many items to fit on one row? There hasn’t been a simple way to allow your chain to expand to multiple rows of items.

create and layout buttons that will control deckītLayout( new GridLayout( 2, 2 ) ) įor ( int i = 0 i < controls.ConstraintLayout Flow: Simple Grid Building Without Nested LayoutsĬonstraintLayout chains are great, but they only work for one row of items. JLabel label3 = new JLabel( "card three" ) Ĭard3.add( new JButton( "North" ), BorderLayout.NORTH ) Ĭard3.add( new JButton( "West" ), BorderLayout.WEST ) Ĭard3.add( new JButton( "East" ), BorderLayout.EAST ) Ĭard3.add( new JButton( "South" ), BorderLayout.SOUTH ) Ĭard3.add( label3, BorderLayout.CENTER ) ĭeck.add( card3, label3.getText() ) // add card to deck set up card3 and add it to JPanel deck JLabel label2 = new JLabel( "card two", SwingConstants.CENTER ) ĭeck.add( card2, label2.getText() ) // add card to deck set up card2 and add it to JPanel deck JLabel label1 = new JLabel( "card one", SwingConstants.CENTER ) ĭeck.add( card1, label1.getText() ) // add card to deck set up card1 and add it to JPanel deck Public class FlowLayoutDemo extends JFrame You can align the components left, right or center (default). This is the most basic layout manager, components are placed from left to right as they were added, when the edge is reached the components are put on the next line. unlike GridLayout each component size can vary and components can be added in any order. Allows components to be arranged left-to-right or top-to-bottom in a container
