I have a custom view (extends View) and I would like to add controls (buttons/text boxes etc) in the OnCreate function to add these components to the view at runtime:
public Section(Context context) {
super(context);
this.setBackgroundColor(Color.argb(255, 242, 242, 242));
this.setOnTouchListener(mSectionOnTouch);
LinearLayout l = new LinearLayout(this.getContext());
l.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this.getContext());
btn.setId(1);
btn.setText("btn1");
l.addView(btn);
Button btn2 = new Button(this.getContext());
btn2.setId(2);
btn2.setText("btn2");
l.addView(btn2);
} // Section
but this does not seem to do anything... Could someone tell me what im doing wrong?
Many thanks
FR
You never add l
to your view. It should look like this:
public Section(Context context)
{
// setup linear layout
addView(l);
}
A slightly simpler way to do this would be to have your custom view extend LinearLayout. Then you can just add the views directly to your custom view and you don't have to nest another container, which is better for performance.