The best way is to use a FlexboxLayout provide by google.
implementation 'com.google.android:flexbox:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Then you have to follow several steps.
1) Add a recycler view, it is the best way to map your data with flexbox layout
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp" />
2) Then provide your own adapter
public class LinkedListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private final LayoutInflater mInflater;
private List<String> mItems = Collections.emptyList();
LinkedListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.my_layout_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((ViewHolder)holder).updateWithItem(this.mItems.get(position), this.callback);
}
@Override
public int getItemCount() {
return mItems.size();
}
void setItems(List<String> items){
mItems = items;
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder{
private final TextView itemText;
private ViewHolder(View view) {
super(view);
itemText = view.findViewById(R.id.item);
}
public void updateWithItem(String item){
itemText.setText(item);
}
}
public List<String> getItems() {
return mItems;
}
}
3) As you can see now you should have something to define your layout, my_layout_item.xml should be placed in layout folder
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:padding="15dp"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4) Now in your fragment or activity you have to linked all this stuff.
myReclyclerView = view.findViewById(R.id.recycler_view);
FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(getActivity());
flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
flexboxLayoutManager.setJustifyContent(JustifyContent.CENTER);
flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
myReclyclerView .setLayoutManager(flexboxLayoutManager);
List<String> items = new ArrayList<String>();
items.add("test");
myAdapter = new LinkedListAdapter (getContext());
myReclyclerView.setAdapter(myAdapter);
myAdapter.setItems(items);
Here we are, that works the best solution to me.
Source in Kotlin from : https://medium.com/gett-engineering/android-div-like-flexbox-layout-55f0e1286d66