Short and sweet summary:
When changing a class from View-derived to ViewGroup-derived, the height and/or width params passed to onSizeChanged are not consistent for my class when used inside of a LinearLayout. Is there any additional information I need to supply when using ViewGroup as a super class to achieve consistent results?
Long version:
I have created a custom View-derived visual control that I’m using like a custom button. I derive from View and then add it to my layouts like this:
class declaration:
public class GameButton extends View {
xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<com.myname.myapp.controls.GameButton
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
I want to get a little fancier with the control, so I have changed it to derive from ViewGroup or any subclass of ViewGroup (currently trying to make it a RelativeLayout, for example). As soon as I do this, it disappears.
I added logging to see the values being passed to onSizeChanged, and I’m getting 0 height being passed to onSizeChanged for every instance of my control with no change to the layout xml (above) that works otherwise when I derive directly from View.
Do I need to add any additional hints, params or layout parameters either to the xml or in code when switching from View to ViewGroup?
You need to implement onMeasure() to tell the toolkit that size you are (you must call setMeasuredDimension() at the end of onMeasure().) ViewGroup, by default, compute their size based on their children and you don’t have any here, so the size is 0x0.