Reputation: 5566
I have a some experience with Android dev but I'm totally lost in a simple task (At least it looks simple).
I have an ArrayList of Strings and I want to create buttons from Strings from the list on the layout. It doesn't have to be button - it can be any clickable object (with text).
The main problem is that I want to create them one after another, and when they dont fit on the screen - new line should be created.
Normal Linear Layout can arrange them horizontaly but will not create a new line. I also tried grid view and its almost it - but it's a grid so all colums are the same size (but texts can be different so I dont like it).
Any Ideas how to do this? Thanks in advance.
Upvotes: 0
Views: 470
Reputation: 711
You could try something like this.
// get the width of the screen
Display display = getWindowManager().getDefaultDisplay();
int windowWidth = display.getWidth();
// keep track of the width of your views on the current row
int cumulativeWidth = 0;
// the width of your new view
int newWidth = // insert some number here based on the length of text.
// get your main layout here
ViewGroup main = (ViewGroup)findViewById(R.id.YOUR_HOLDING_LAYOUT);
// the linear layout holding our views
LinearyLayout linear = null;
// see if we need to create a new row
if (newWidth + cumulativeWidth > windowWidth){
linear = new LinearLayout(this);
// set you layout params, like orientation horizontal and width and height. This code may have typos, so double check
LayoutParams params = new LayoutParams(LinearLayout.FILL_PARENT, LinearLayout.WRAP_CONTENT);
params.setOrientation(HORIZONTAL); // this line is not correct, you need to look up how to set the orientation to horizontal correctly.
linear.setParams(params);
main.addView(linear);
// reset cumulative width
cumulativeWidth = 0;
}
// no create you new button or text using newWidth
View newView = ... // whatever you need to do to create the view
linear.addView(newView);
//keep track of your cumulatinv width
cumulativeWidth += newWidth;
Upvotes: 1
Reputation: 36302
There's no flow layout in Android. You'll have to either implement your own custom layout (not trivial) or find a third party flow layout.
Upvotes: 1