Android CardView center horizontally

Samuel Navarro picture Samuel Navarro · Sep 28, 2017 · Viewed 8.2k times · Source

I have read several answers on how to align a card view horizontally. I am trying to achieve the following:

Desired Result

This is so far the code I have written (I have followed around 20 answers from different posts, none which have helped me. The problem is the margin or space on the left and the right, I need that space, but the card view expands the whole width.

<RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true">

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignStart="@+id/mProjectCDCNumCardView"
                android:layout_marginStart="7dp"
                android:layout_marginTop="7dp"
                android:text="DETAIL"
                android:textColor="#000"
                android:textSize="16sp" />

            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/mProjectCDCNumCardView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="13dp"
                card_view:cardCornerRadius="2dp"
                card_view:contentPaddingLeft="50dp"
                card_view:contentPaddingRight="@dimen/activity_horizontal_margin"
                card_view:contentPaddingTop="20dp"
                card_view:contentPaddingBottom="@dimen/activity_vertical_margin"
                android:background="@drawable/cardview_normal"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/textView8">

                    <EditText
                        android:id="@+id/mProjectCDCNumEditText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@drawable/editborder"
                        android:hint="Project Number"
                        android:inputType="textPersonName"
                        android:padding="10dp" />


            </android.support.v7.widget.CardView>

            <TextView
                android:id="@+id/mProjectAddressLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/editborder"
                android:layout_below="@id/mProjectCDCNumCardView"
                android:ems="16"
                android:padding="10dp"
                android:hint="Location"
                android:textColor="#000"
                android:textSize="18sp"
                android:drawableRight="@drawable/left_arrow"/>



        </RelativeLayout>

Thanks!

Answer

Chili Pepper picture Chili Pepper · Sep 29, 2017

You just add android:layout_marginStart and android:layout_marginEnd to your cardview.