Julia Zhao Julia Zhao - 2 months ago 5x
Android Question

Why is only one button appearing when I dynamically add two?

I have this code in a method where

definitely has two teams.

for (Team t : listOfTeamsJoined)

which calls this method:

private void makeTeamButton(String teamName)
     Button newTeamButton = new Button(this);
       LinearLayout teamButtonHolder = (LinearLayout) findViewById(R.id.availableTeamsList);

       newTeamButton.setLayoutParams(new LinearLayout.LayoutParams(

       newTeamButton.setTextColor(Color.parseColor("#FFFFFF "));


The problem is that only one of them will show. I suspect that either one is being replaced by the other or they are appearing on top of each other. However, since I am using
, I don't think they should be appearing on top of each other and as far as I know,
doesn't replace views, it just appends them. However, if I reorder the list then I can make the other appear, they just won't appear together.


The default orientation is horizontal. And you're using a width of MATCH_PARENT for the widths of both buttons. This means that your first button will take the whole width leaving nothing for the second one, thus making it not visible.

Set the orientation manually if you want your LinearLayout to be vertical like this:


If you wish to keep it horizontal, then just invert the width and height to be WRAP_CONTENT and MATCH_PARENT, respectively. And that'll work too but it'll show them side by side.