fraggjkee fraggjkee - 4 months ago 13
Android Question

Implementing custom View and/or ViewGroup (Flow Layout) in Android

I'm trying to achieve something similar to this:

widget demo

So, this is a container which can hold an arbitrary number of children. Each child is a simple text with border. Children may differ by their width. I assume this part can be implemented by extending the

TextView
widget (since it isn't clickable). Much more interesting thing here is the container for these bordered text views. It should support a regular
addView(child)
operation which automatically aligns the child according to the simple rule: if there is enough room within a current row - place child to the right of the last item in row. If there is no space - move child to the next row. So the basic logic seems quite similar to
TextView
or
EditText
: if text is too long, we display it in several lines. My idea is too extend
RelativeLayout
, but I still hope to find something easier.

The scheme I've described above (custom container + custom
TextView
) is not a mandatory, I'm just looking for ways to achieve this. Any thoughts guys?

Answer

Seems that pattern I was looking for is called FlowLayout (thanks to nitzanj). There are several of implementations of it:

If you decide to write your own version, here are helpful guides about writing custom views: