Search code examples
android-recyclerviewcardview

android how to center a TextView within the full width of the parent?


I have a Checkbox and a TextView that are anchored to the left side of a UI. I have another TextView "newcard #" that I would like to center in the full width of the parent. The parent width is the white space running from left to right, shown below. Notice how "newcard #" is to the right of the red center line. Basically I would like "newcard #" to be centered around the red center line.

enter image description here

I have tried multiple combinations of gravity and layout_gravity without any luck. What am I missing here?

item.xml

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="4dp" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_selector"  >

<CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        android:gravity="center"
        android:background="#008080"  />

    <TextView
        android:id="@+id/cardType1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_toRightOf="@+id/chkSelected"
        android:layout_toEndOf="@+id/chkSelected"
        android:paddingStart="3dp"
        android:paddingLeft="3dp"
        android:paddingEnd="6dp"
        android:paddingRight="6dp"
        android:layout_marginTop="4dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold|italic"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    <TextView
        android:id="@+id/cardBlankText1"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_toRightOf="@+id/cardType1"
        android:layout_toEndOf="@+id/cardType1"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="6dp"
        android:layout_marginStart="6dp"
        android:gravity="center_horizontal"
        android:textColor="@color/colorFlLabelFinal"
        android:textStyle="bold"            
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />  

Solution

  • I kept only RelativeLayout, hope a result won't change. Deleted:

    android:layout_toEndOf="@+id/cardType1"
    android:layout_toRightOf="@+id/cardType1"
    

    and inserted:

    android:layout_centerHorizontal="true"
    

    So:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_selector">
    
        <CheckBox
            android:id="@+id/chkSelected"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:layout_marginTop="4dp"
            android:background="#008080"
            android:gravity="center"/>
    
        <TextView
            android:id="@+id/cardType1"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginTop="4dp"
            android:layout_toEndOf="@+id/chkSelected"
            android:layout_toRightOf="@+id/chkSelected"
            android:gravity="center"
            android:paddingEnd="6dp"
            android:paddingLeft="3dp"
            android:paddingRight="6dp"
            android:paddingStart="3dp"
            tools:text="Work"
            android:textColor="#ffffff"
            android:textStyle="bold|italic"/>
    
        <TextView
            android:id="@+id/cardBlankText1"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="6dp"
            android:layout_marginStart="6dp"
            android:layout_marginTop="4dp"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            tools:text="newcard#"
            android:textColor="@color/colorFlLabelFinal"
            android:textStyle="bold"/>
    
        <TextView
            android:id="@+id/cardBlankTextNumsTotal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="fggfhfg"
            android:layout_toRightOf="@id/cardBlankText1"/>
    
    </RelativeLayout>