1

I am trying to create a method where each time I click a button, I want the screen to display a Relative Layout box below one that is currently there or create one at the top if there isn't one there and I want it to keep creating them below from each button click. Currently using this method, nothing happens when the button is clicked.

Here is the code that I have so far and can someone please help me see what I am doing wrong and what I can do to fix this issue:

public void createNewLayout(){

    int currentId = 10;

    for(int i = 1; i <= numberOfLayouts; i++){
        newLayoutContainer = new RelativeLayout(this);
        newLayoutContainer.setId(currentId);

        if(currentId == 10){
            newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
            newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, containerLayout2.getId());
            newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout2.getId());
            newLayoutParams.addRule(RelativeLayout.BELOW, containerLayout2.getId());
            newLayoutParams.setMargins(0, 0, 0, dpMargin);
            newLayoutContainer.setBackgroundResource(R.color.display_panels);
        }

        else{
            newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
            newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, newLayoutContainer.getId());
            newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, newLayoutContainer.getId());
            newLayoutParams.addRule(RelativeLayout.BELOW, currentId-1);
            newLayoutParams.setMargins(0, 0, 0, dpMargin);
            newLayoutContainer.setBackgroundResource(R.color.display_panels);
        }

        newLayoutContainer.setLayoutParams(newLayoutParams);
        layout.addView(newLayoutContainer);

        currentId++;
    }

}
James Meade
  • 1,147
  • 5
  • 22
  • 46
  • I would use a `LinearLayout` with vertical orientation as the parent/holder layout. Then just adding the children will ensure the correct layout without any further code. – Ken Wolf Jul 09 '13 at 12:31
  • This question is similar to this one and I used it to help create my method: http://stackoverflow.com/questions/14139214/android-dynamically-add-layouts-under-each-other?rq=1 – James Meade Jul 09 '13 at 12:33
  • layout is a RelativeLayout – James Meade Jul 09 '13 at 12:38
  • This is because I have textviews above the boxes that are relative to each other and relative to the boxes. – James Meade Jul 09 '13 at 12:39
  • And `layout` is properly attached to your activity? You've intialised it via XML, etc? – Ken Wolf Jul 09 '13 at 12:39
  • I didn't refer it to an XML, but instead created a new RelativeLayout programmatically, but that works fine and is how I wanted it to be. – James Meade Jul 09 '13 at 12:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33136/discussion-between-james-meade-and-ken-wolf) – James Meade Jul 09 '13 at 13:00

0 Answers0