title: getChildCount() returns 0 when called on TableLayout
body: <p>This question is related to one I posted earlier. <a href="https://stackoverflow.com/questions/26055707/views-removed-using-removeview-are-there-when-i-next-open-the-activity-androi?noredirect=1#comment40819352_26055707">Views removed using removeView() are there when I next open the Activity (Android)</a></p>
  
  <p>Background: When a user logs into my app they are taken from the login activity to the mainpage activity. The mainpage has a TableLayout that contains dynamically generated buttons. However if the user logs out and back in again, all of these buttons are repeated so I am trying to find out how best to remove these buttons after they are generated. In my previous post it was suggested I remove the buttons at the very start of the main page activity, before the new ones are drawn, so this is what I am trying to implement.</p>
  
  <p>However when I call getChildCount() on this layout it does not always return the correct answer.</p>
  
  <p>So far, here is the code that is run at the start of the main page activity:</p>
  
  <pre><code>        TableLayout tableLayout = (TableLayout)findViewById(R.id.MainPageTableTitle);
          //removeSectionButtons(tableLayout);  this is where i am trying to remove the buttons
          System.out.println("there are  oncreate " + tableLayout.getChildCount());
          drawButtons(tableLayout);
          System.out.println("there are  ondraw " + tableLayout.getChildCount());
  </code></pre>
  
  <p>The first print line returns 0 and the second print line always returns the correct answer (number of buttons drawn including all of the repeated ones). But I am not sure why getChildCount() returns the wrong answer the first time. If anyone can explain I would be incredibly grateful</p>
  
  <p>My drawButtons() method is as follows (it draws two buttons per row):</p>
  
  <pre><code>     public void drawButtons(TableLayout tableLayout){
      //get the number of buttons
      int noOfButtons = mySectionTableHandler.getSectionDetails().size();
      //calculate the number of rows needed (there are 2 columns)
      //set flag to say if buttons are odd as it affects how many are drawn
      int noOfRows;
      boolean evenNoOfButtons;
      if(noOfButtons % 2 == 0){
          //even no of buttons
          noOfRows = noOfButtons/2;
          evenNoOfButtons = true;
      } else {
          //odd no of buttons
          noOfRows = (noOfButtons+1)/2;
          evenNoOfButtons = false;
      }   
  
      //counter to give each button a unique id
      int counter = 1;
  
      for(int i = 0; i&lt;noOfRows;i++){
          TableRow newRow = new TableRow(this);
  
          Button a = new Button(MainPageActivity.this);
          a.setId(counter);
          sectionButtons.put(counter, a);
          counter++;
          newRow.addView(a);
  
          //if there are even buttons OR if there are an odd no
          //of buttons but this isn't the last row then add 
          //second button to row
          if(evenNoOfButtons || (!evenNoOfButtons &amp;&amp; (noOfRows-1!=i))){
              Button b = new Button(MainPageActivity.this);
              b.setId(counter);
              sectionButtons.put(counter, b);
              counter++;
              newRow.addView(b);
          }       
          tableLayout.addView(newRow);
      }   
  
  }
  </code></pre>