Jacob Alley Jacob Alley - 9 days ago 5
Android Question

Android layout cannot center

Here is my xml in the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cwigreen">
<LinearLayout
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height ="wrap_content"
android:background ="@drawable/customCard"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height ="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height ="match_parent">
<TextView
android:text="Policy Holder Name: "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor ="@color/black"
android:textSize="14dip" />
<TextView
android:text="ID Number: "
android:textSize="14dip"
android:layout_centerHorizontal="true"
android:textColor ="@color/black"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height ="match_parent">
<TextView
android:id="@+id/holderName"
android:text=""
android:layout_centerHorizontal="true"
android:textColor ="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dip" />
<TextView
android:id="@+id/idNumber"
android:text=""
android:layout_centerHorizontal="true"
android:textColor ="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dip" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>


here is a picture of the output enter image description here

I need the textviews to show up in the middle of the card background image. I have tried using a grid layout and a number of other settings. Any help would be greatly appreciated.

Answer

You should try this xml structure. ( I don't have your background in example image )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cwigreen">
<LinearLayout
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height ="wrap_content"
    android:background ="@drawable/customCard"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="Policy Holder Name: "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"
            android:textColor ="@color/black"
            android:textSize="14dp"
            android:id="@+id/textView1" />

        <TextView
            android:text="ID Number: "
            android:textSize="14dp"
            android:layout_centerHorizontal="true"
            android:textColor ="@color/black"
            android:textStyle="bold"
            android:layout_below="@id/holderName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2" />

        <TextView
            android:id="@+id/holderName"
            android:text=""
            android:layout_centerHorizontal="true"
            android:textColor ="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/textView1"
            android:textSize="14dip" />

        <TextView
            android:id="@+id/idNumber"
            android:text=""
            android:layout_centerHorizontal="true"
            android:textColor ="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/textView2"
            android:layout_alignBottom="@id/textView2"
            android:textSize="14dip" />
    </RelativeLayout>
</LinearLayout>

Result like below picture.

enter image description here

Please ask any additional questions