danny
danny

Reputation: 1299

android: add components dynamically to layout

I have a java app that adds views dynamically to a container panel as follows.

void addBoard(int ID) {
    BoardPanel p = new BoardPanel(myManager,ID);
    setAutoLayout();
    containerPanel.add(p);
    containerPanel.repaint();
}

When I try to convert this to an android app it hangs when addView is called. What is the problem? Note that the user could add a 1000 views (BoardPanels) if he likes so I can not use XML layouts.

void addBoard(int ID) {
    BoardPanel p = new BoardPanel(context,myManager,ID);
    Log.i("Info", "Going to add view");
    containerPanel.addView(p);
    Log.i("Info", "Added");
    containerPanel.postInvalidate();
}

Thanks


Update: Problem seems to be due to threaded code as Aegonis pointed out.

Upvotes: 0

Views: 7663

Answers (1)

gleerman
gleerman

Reputation: 1813

Try ViewGroup.addView() (FrameLayout, GridLayout, LinearLayout, ... are all extensions of ViewGroup).

For example, if you want a View to be inserted after the first already existing View:

LinearLayout layout = (LinearLayout) findViewById(R.id.layoutID);
layout.addView(viewToBeAdded, 1);

Upvotes: 1

Related Questions