0

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

Kabilan
  • 191
  • 10
poutsa123
  • 3
  • 2

2 Answers2

1

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>
AlexTa
  • 5,133
  • 3
  • 29
  • 46
  • Due to a design change it seems to have centered itself. The change was adding these values that rounded it and gave it a backgroud. The problem now is that the text cannot center correct with its bg. `android:background="@drawable/rounded" android:ems="10" android:alpha="0.7"` – poutsa123 Jul 08 '20 at 08:10
0

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841