poutsa123
poutsa123

Reputation: 3

How do i center the items of a Recycler View?

i have managed to make a RecyclerView list but am unable to center the text inside. Below are the codes:

RecyclerView:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Item:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingBottom="15dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I have tried all properties i could find,alignText, gravity, alignLayout etc

Upvotes: 0

Views: 645

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363567

In the item layout just use:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        .../>

</LinearLayout>

enter image description here

Upvotes: 0

AlexTa
AlexTa

Reputation: 5251

Just change Item layout root element width property from wrap_content to match_parent, so each row item takes full RecyclerView's width and child text would be able to be centered on that width:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" // this is the change you have to make
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingBottom="15dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions