Search code examples
androidlayoutandroid-linearlayoutandroid-constraintlayoutonmeasure

lines break of 3 textView items according to their length


Problem have this UI problem:

1) I have 3 textViews which I want to hold in one parent view.

2) I want to populate as many items as possible in as few lines as possible.

3) Only between 2 items there is a separator.

Suggested algorithm:

Get the container's widthGet each item unspecified width (For that I have to create a custom textView to override onMeasure()).

Calculate and keep aside "last line remaining width".

If next textView can fit in the last line remaining width: 

  • make the last separator visible
  • put it in the same line and move to next textView item

else:

  • open a new line with remaining width = (container's width - current item's width)
  • make the last separator invisible
  • put the current item in that line (break lines if needed)

I have two options:

  1. To use a vertical linear layout of horizontal linear layouts.
  2. To use a constraint layout.

I would fill a List of Lists and will populate the horizontal linear layouts via code.

Or fill the constraint layout via code (less generic and I guess more difficult).

Is there any simpler solution?

enter image description here


Solution

  • If I'm understanding your requirements correctly, I believe the FlexboxLayout library from Google should do exactly what you want.

    You'd probably use a FlexboxLayout (rather than a RecyclerView with a FlexboxLayoutManager), but it is good to note that this library also supports recycling.