I searched here on SO, on Google, on the android docs...
But I cannot find a single snippet of code with a example of custom viewgroup, I find at most some vague explanations...
Can someone provide one? How you make a viewgroup where you can put its children where you want?
In Android, there are actually two other Views readily available to do this: Spinner and AutoCompleteTextView , but regardless, the concept of a Combo Box makes an easy-to-understand example. To create a compound component: The usual starting point is a Layout of some kind, so create a class that extends a Layout.
Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.
View are typically created to provide a user interface experience with is not possible with the default views. Using custom view allows the developer allow to do certain performance optimization, i.e., in case of a custom layout the development can optimize the layout manager for his use case.
I think the simplest example to look at is the source for AbsoluteLayout.java
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/AbsoluteLayout.java
You need to override onMeasure to measure the children and onLayout to position them.
I have strikingly more complicated ViewGroup code I can share as well if you want.
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