Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout - How to dynamically add views and break line based on empty space

Tags:

android

I would like to have a Layout for Views in Android that manages itself to use its empty space dynamically and EITHER puts the next view added right to the last view if it still fits OR breaks line and adds the view on the new line on the left...

Example:

||Name|LoooooongName|Ho      ||
||SuperLongName|NextLongname ||
||Bob|Sue|Martin|Richard|Joe ||
||Marvin|Homer|Ann-Marie     ||

Any clues? Thanks for your help!

like image 937
johannes Avatar asked Mar 01 '11 15:03

johannes


People also ask

Is this possible to add views in a layout dynamically in Android?

This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

Can we add or delete any view type in XML on runtime?

Yes, because you can not add/delete any View Type in XML on runtime. Dynamic layouts are developed using Java and can be used to create layouts that you would normally create using an XML file.

How do I remove View Kotlin?

Kotlin Extension SolutionAdd removeSelf to directly call on a view. If attached to a parent, it will be removed. This makes your code more declarative, and thus readable.


1 Answers

This is possible, but not easy. You will have to create a custom layout. Check out the source code to LinearLayout for an example, specifically layoutHorizontal().

You will override ViewGroup.onLayout(). Inside it you will check the size of your children using View.getMeasured{Width,Height}(), making sure they can fit on the line or move to the next.

like image 190
Matthew Willis Avatar answered Nov 09 '22 03:11

Matthew Willis