RecyclerView GridLayoutAdapter - How to center items

10,107

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

Share:
10,107
Ralf
Author by

Ralf

Updated on June 14, 2022

Comments

  • Ralf
    Ralf almost 2 years

    I am using RecyclerView with GridLayoutAdapter. Is it possible to center items horizontally or vertically?

    My item layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:gravity="center"
                  android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/item_photo_image_view"
            android:layout_width="@dimen/custom_photo_item_size"
            android:layout_height="@dimen/custom_photo_item_size"
            android:background="@drawable/custom_image_background"
            tools:src="@drawable/gallery_list_cars"/>
    
    </LinearLayout>
    

    Actually it looks like this: http://oi61.tinypic.com/34pjmtc.jpg

    Desired layout should look like: http://oi62.tinypic.com/7e2t.jpg

    Setting RecyclerView to wrap_content does not work, it still uses all free space. I imagine it should be LayoutManager responsibility to setup children gravity within RecyclerView.