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!
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With