0

I'm currently using a Card View inside a recycler and the text is left-alligned despite my efforts.

Here is the Book Item Layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:cardCornerRadius="4dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/bookTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_gravity="center_horizontal"
            />
    </LinearLayout>
</androidx.cardview.widget.CardView>

Here is the recycler itself.

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/bookRecycler" />

This is the current output. enter image description here

As you can see the text is not centered.

I tried using layout gravity to center the text which didn't work. I also tried using a constraint layout on the text view and using constraints to center the text without any luck.

android:layout_gravity = "center" has already been attempted.

Any advice on what I'm overlooking would be great. Thank you.

2 Answers 2

2

add android:gravity="center" for LinearLayout and you are good to go

I see your LinearLayout have only one child and no special attributes, so you can even remove whole LinearLayout - place TextView straight inside CardView, which extends FrameLayout. For centering childs inside FrameLayout you may also use android:gravity="center" for parent and/or android:layout_gravity="center" for child

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center"
    app:cardCornerRadius="4dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/white">

    <TextView
        android:layout_gravity="center"
            ... rest of code
        
0

android:gravity="center" is the correct code, instead of android:layout_gravity="center_horizontal"

2
  • Yeah that doesn't work either, I already tested that, forgot to mention that. Thank you.
    – Dovita
    Jul 22, 2021 at 9:23
  • If TextView android:layout_width="match_parent" then android:gravity="center" works
    – Sniffer
    Jul 22, 2021 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.