Can someone help me with my Syntax/Methodology? I would like to set up a bunch of different views in a viewgroup. My code fails at the addView method.
Unfortunately, I can’t find jack for examples (which is how I learn) using the ViewGroup class online.
Thanks all.
public class TileView extends ViewGroup {
private TestClass mTestClass;
public TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initViews();
}
public TileView(Context context, AttributeSet attrs) {
super(context, attrs);
initViews();
}
public void initViews() {
addView(mTestClass);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
}
}
public class TestClass extends View{
public TestClass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TestClass(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestClass(Context context) {
super(context);
}
}
First of all you should check your own layout xml file. By error message line number 6 is wrong. second problem is you didn’t initialize mTestClass object in your view group source code. If you want to get children view, you can use getChildAt(int) method in view group. It is simple example,
And, is it full source code? I think you should implement onMeasure and onLayout method in ViewGroup and View instance.