Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom ViewGroup example?

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?

like image 421
speeder Avatar asked Apr 14 '11 12:04

speeder


People also ask

What are the types of custom views?

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.

What is customized view?

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.

Why do we need a custom view?

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.


1 Answers

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.

like image 161
slund Avatar answered Oct 11 '22 15:10

slund