Search code examples
javaandroidandroid-recyclerviewcenterandroid-cardview

Centre text inside a Cardview - Android Studio


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.


Solution

  • 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