Reputation: 502
I have this code in a method where listOfTeamsJoined
definitely has two teams.
for (Team t : listOfTeamsJoined)
{
makeTeamButton(t.display_name);
}
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(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
newTeamButton.setBackgroundColor(Color.parseColor("#00C272"));
newTeamButton.setTextColor(Color.parseColor("#FFFFFF "));
newTeamButton.setText(teamName);
teamButtonHolder.addView(newTeamButton);
}
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 LinearLayout
, I don't think they should be appearing on top of each other and as far as I know, addView
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.
Upvotes: 0
Views: 65
Reputation: 1761
As you set your buttons width as match_parent
like below:
newTeamButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
you have to set your LinearLayout
's orientation as vertical
;
But if you want to set those buttons as horizontally
then set the width param as wrap_content
like:
newTeamButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
Upvotes: 0
Reputation: 7479
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:
teamButtonHolder.setOrientation(LinearLayout.VERTICAL);
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.
Upvotes: 1
Reputation: 1697
You are add two new button dynamically, But it show one button while you see, because it happened due to overlap to each others. so give some margin or padding to each others, then you will see it. I hope it would be help to you.
Upvotes: 0
Reputation: 3861
What orientation has your LinearLayout? Seems to be it's set to horizontal and you want them placed on top of each other so "vertical" should be the right choice here.
Upvotes: 0