johanvs
johanvs

Reputation: 4393

Put views in an Android layout with automatic line break

I want to put fixed-dimension buttons centred in a layout so that they automatically go to the next line when they are too many. But I know that this behaviour doesn't work with LinearLayout.

Do you have an idea on how I can proceed?

Here is the behaviour that I would like:

enter image description here

Upvotes: 9

Views: 3704

Answers (3)

Ari
Ari

Reputation: 1470

The Flexbox library from Google does exactly this:

Add the dependency to app/build.gradle:

dependencies {
    implementation 'com.google.android:flexbox:2.0.1'
}

Usage of the layout:

<com.google.android.flexbox.FlexboxLayout
    app:flexWrap="wrap">
        <View/>
        <View/>
        <View/>
        <View/>
</com.google.android.flexbox.FlexboxLayout>

Upvotes: 9

johanvs
johanvs

Reputation: 4393

I finally proceeded dynamically : I created a LinearLayout for each line, and calculated the width taken by the rectangles compared to the global layout width to calculate the number of lines needed and to put each rectangle in the good line.

Upvotes: -1

user3540792
user3540792

Reputation: 56

This is the GridView behavior.

Upvotes: -3

Related Questions