How to Center Items of a Recyclerview

How to center items of a recyclerview?

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

Sample Image

How to center Items in Recyclerview?

Set the list item layout to match_parent like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rLayMainContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />

<TextView
android:id="@+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>

How to center items in a Recyclerview?

Since you're using GridLayoutManager set card_view.xml parent view width as match_parent i.e.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" //change
android:layout_height="wrap_content"
android:gravity="center"//add this
android:orientation="vertical">

Or you can make that RelativeLayout and set child CardView centerInParent to true

Comment in case of any queries

Hope this helps. Happy coding!

How to center text in RecyclerView

You seem to be using a GridLayoutManager(...) instead of LinearLayoutManager(this). You're only displaying a simple list, so I recommend you use Linear.

Also there's a matter of how you did your list item layout. Did you assign a static width to your layout? Have you set it to layout_centerInParent if you're using RelativeLayout?

How to center RecyclerView items horizontally with vertical GridLayoutManager

Try letting the column item fill the width of the column while centering everything inside:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">

<ImageView
android:id="@+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"/>

<TextView
android:id="@+id/movie_column_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>

RecyclerView GridLayoutAdapter - How to center items

You already did with that line:

android:gravity="center"

With that line you already Center the ImageView horizontal and vertical.

If you just want one of them use for example:

android:gravity="center_horizontal"

I don't know what exactly is your Problem, we do not even know

android:layout_width="@dimen/custom_photo_item_size"

which value is declared in your dimensions.xml...

If you want to Center your whole Item you would Need to declare:

android:layout_gravity="Center"

but this doesn't make any sense as you only have one layout in a GridView-Cell and you declared your rootView of your Item as

android:width="match_parent"
android:height="match_parent"

So this is just an info for you ;)

Provide more Infos if you want better Support. Hope it helps anyway



Related Topics



Leave a reply



Submit