Search code examples
androidandroid-layoutandroid-recyclerview

How to center items of a recyclerview?


I need to center elements in each row so they will be like in this mockup. I've been searching if there is any layout that works that way without look. items are centered in their rows.

mockup

This is how it looks now in my code.enter image description here


Solution

  • Achieved with:

    recyclerView.adapter = MyAdapter()
    val layoutManager = FlexboxLayoutManager(context).apply {
        justifyContent = JustifyContent.CENTER
        alignItems = AlignItems.CENTER
        flexDirection = FlexDirection.ROW 
        flexWrap = FlexWrap.WRAP
    }
    recyclerView.layoutManager = layoutManager
    

    You need to add FlexboxLayout to your gradle:

    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    

    enter image description here