1

Does someone know why the following code doesn't center the text in the button

but the second one does ?

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/gotItButton"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:background="@drawable/btn_selector"
        android:padding="0dp" />

    <com.m.view.text.MyTextView
        android:id="@+id/gotItText"
        style="@style/textOnBg"
        android:layout_marginTop="25dp"
        android:text="Got it"
        android:textColor="#00bcfe"
        android:textSize="16dp"
        android:textStyle="italic" />
</RelativeLayout>

aren't gravity in parent and layout_gravity in each child are the same

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/gotItButton"
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:layout_centerInParent="true"
            android:background="@drawable/btn_selector"
            android:padding="0dp" />

        <com.m.view.text.MyTextView
            android:id="@+id/gotItText"
            style="@style/textOnBg"
            android:layout_marginTop="25dp"
            android:layout_centerInParent="true"
            android:text="Got it"
            android:textColor="#00bcfe"
            android:textSize="16dp"
            android:textStyle="italic" />
    </RelativeLayout>
Onik
  • 19,396
  • 14
  • 68
  • 91
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Well, for starters, the first code contains no gravity attributes in the TextView and the second one does. Since you RelativeLayouts width is set to match_parent, how can we know that the text isn't just filling up the entire width anyhow? – Snailer Dec 17 '13 at 14:51
  • You did not do anything about centering TextView on the first one. `android:gravity="center"` on RelativeLayout is not about your children in it. – Batuhan Coşkun Dec 17 '13 at 14:51

1 Answers1

7

gravity is inside your button. layout_gravity is outside (centers in parent)

So, you don't need gravity in your RelativeLayout, move it in Button.

And remove layout_gravity from button, since you use centerInParent.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • gravity is inside - so when in RelativeLayout it should effect all its children. no? – Elad Benda Dec 17 '13 at 15:08
  • In theory. But for that you'd better use centerInParent (if parent is RelativeLayout) or layout_gravity (if parent is a LinearLayout) on the children. Note that a child can also be another container, so... no problem at all – Phantômaxx Dec 17 '13 at 15:11