0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Any clues how to start with this "not trival" solution? :)
You might've tried Googling. Here are two links that came up when I searched for Android flow layout: another SO question and a GitHub project.
1

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;

1 Comment

This might work but the biggest problem here is: "int newWidth = // insert some number here based on the length of text." This will work differently on different displays when you use dpi instead of px as units. You might be able to work out this number but possibly there would be a screen/dpi where you would get half of the button at the end of the line :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.