1

I got this code, a linearlayout inside relativelayout... however its not centering horizontally... any idea why?

i want the linearlayout to be horizontall centered. i am filling it with images via code.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/relative1"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="10"
        android:background="@color/list_background_pressed" >
        <LinearLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerHorizontal="true" >           
        </LinearLayout>
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="HI! Welcome to My Zain"
            android:textColor="#ffffff" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/text2"
            android:layout_centerHorizontal="true"
            android:paddingTop="20dp"
            android:src="@drawable/ico_profile_1_64x64" >
        </ImageView>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/relative2"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="10" >
         <TextView
            android:id="@+id/text5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="The new Recharge Mechanism"
            android:textColor="#000000" />
    </RelativeLayout>
</LinearLayout>
JPM
  • 9,077
  • 13
  • 78
  • 137
user3278732
  • 1,694
  • 10
  • 31
  • 67

2 Answers2

2

In the LinearLayout, try setting the layout_width to "wrap_content". Right now, you're stretching it to the full width of the window, so there's nothing to center.

android:layout_width="wrap_content"

Alchete
  • 1,629
  • 2
  • 18
  • 29
1

add android:gravity="center"

<RelativeLayout
    android:id="@+id/relative2"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:gravity="center"
    android:layout_weight="10" >
Lucian Novac
  • 1,255
  • 12
  • 18
  • This post was automatically flagged as low quality due to its length. It looks fine to me, but in the future I recommend adding a quick sentence or two explaining what your suggested change does and why it works. – Bryan Herbst Mar 03 '14 at 15:19