Jacob Alley
Jacob Alley

Reputation: 834

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.

Upvotes: 0

Views: 54

Answers (2)

Hamit Enes
Hamit Enes

Reputation: 122

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

Upvotes: 1

ovalb
ovalb

Reputation: 605

Your linear horizontal layout (the second one) should be centered in parent too with the android:layout_centerInParent="true"

Upvotes: 0

Related Questions