0

I have a special requirement in one of my project. I have an ArrayList of items and I want to show the Views like in the image below.

enter image description here

All the brown boxes are View's (TextView or LinearLayout or Button). I want to add them in the order they are numbered. Their width depends on the content inside them i.e. length of text in the case of TextView.

When I add a new view, I want it to be on the right side of the previous view if their is space otherwise it should go in the next line/row.

How can I accomplish this?

galath
  • 5,717
  • 10
  • 29
  • 41
Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54
  • Write your own `ViewGroup` - override `onLayout` and specify coordinates for each child. There are plenty of examples online. – Dmitry Zaytsev Jul 10 '15 at 18:05
  • I would recommend using a `GridLayoutManager` on a `RecyclerView`. You will need to specify the span (i.e. width) for each item somehow, and it looks as though others have worked on this before. See previous answers here: http://stackoverflow.com/questions/26666143/recyclerview-gridlayoutmanager-how-to-auto-detect-span-count – Daniel Smith Jul 10 '15 at 18:14
  • @DanielSmith's approach is interesting, but that seems like too much of an optimization to use a `RecyclerView` if the number of views is relatively small. Consider using Romain Guy's [FlowLayout](https://code.google.com/p/devoxx-schedule/source/browse/devoxx-android-client/src/net/peterkuterna/android/apps/devoxxsched/ui/widget/FlowLayout.java?r=422c38196733ba3c54eb44418160e248ee1aea86) - it positions children based on the available space. This should likely fit your use case – Abhijit Jul 10 '15 at 18:24
  • 1
    I'm assuming that if the items are stored in an arraylist that there are many of them, but in hindsight, my assumption may have been incorrect! OP should maybe add info about the number of items expected. – Daniel Smith Jul 10 '15 at 18:37

2 Answers2

0

I like to use FlowLayout. Super simple to use and doesn't require you re-inventing the wheel.

Just change your root view in the xml file to this:

<org.apmem.tools.layouts.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>

And thats all it takes!

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
0

I used this third party library to solve this.

Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54