Nari Kim Shin
Nari Kim Shin

Reputation: 2499

Dynamically add view in vertical and horizontal way

I'm trying to create Buttons in LinearLayout dynamically, and I want to add those in vertical and horizontal way.

At first, add a button A in the layout, and if there's enough space between button A and screen edge, add button B to the right of button A (horizontally). Otherwise, add button B below button A (vertically).

My current layout :

<LinearLayout
    android:id="@+id/btn_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

in class :

LinearLayout btnLayout = (LinearLayout) findViewById(R.id.btn_layout);
btnLayout.removeAllViewsInLayout();
for(Tag tag : tagList.getChildTags()) {
    Button button = new Button(this);
    button.setId(tag.getId());
    button.setText(tag.getName());
    btnLayout.addView(button);
}

In this case, if I set orientation as horizontal, then some of buttons are not showing (cut-off by screen), and if I set as vertical, it looks pretty bad.

Is there any way to solve this problem? Thanks everyone in advance!

Upvotes: 0

Views: 2064

Answers (1)

nKn
nKn

Reputation: 13761

You can achieve this but not in a trivial way. I'll explain how I do something similar (in my case, I add TextViews) to TableRows, if they fit.

With this approach you'll have to use a TableLayout and add TableRows to it with your Buttons. So you might replace your "@+id/btn_layout" LinearLayout to be a TableLayout instead.

Firstly, to get the screen's width, use something like this:

final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);

final WindowManager.LayoutParams params = getWindow().getAttributes();

// Your screen's width will be stored within your params.width value    

You'll use this to know if the current Button still fits the screen's width within the current TableRow or it has to be added to a new one. So now, use something like this to create your buttons:

int currentRowsWidth = 0;
TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout);
TableRow currentRow = new TableRow();

for (Tag tag : tagList.getChildTags()) {
  Button button = new Button(this);
  button.setId(tag.getId());
  button.setText(tag.getName());

  // There's where you check whether it still fits the current `TableRow` or not
  if (currentRowsWidth + button.getWidth() < params.width) {
    currentRowsWidth += button.getWidth();
    currentRow.addView(button);
  }
  else {
    // It doesn't fit, add the currentRow to the table and start a new one
    tl.add(currentRow);
    currentRow = new TableRow();
    currentRow.addView(button);
    currentRowsWidth = button.getWidth();
  }
}

It might happen that once you get out of the loop there are still Buttons to add in the currentView, simply test it:

if (currentRow.getChildCound() > 0)
  tl.add(currentRow);

I'm writing this from head, so some things might not compile at first time, but I hope you get the idea.

Upvotes: 2

Related Questions