Relative Layout Trick

One can easily center a single View horizontally in a layout with the android:layout_centerHorizontal=”true” property.  However, how does one center two Views horizontally in a layout?  For instance, two buttons side-by-side.  This is also very easy, yet may not be obvious.  Simply create an “anchor” view that will never be seen by the user (visibility), and center it horizontally with the above attribute.  Now this View may serve as a center anchor for all other sibling Views you create in the Relative layout.  Now you can assign Views “toLeftOf” or “toRightOf” this centered anchor View.  Example:
    <TextView
        android:id=”@+id/anchor”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=””
        android:layout_centerHorizontal=”true”
        android:visibility=”invisible” />
<Button
        android:id=”@+id/saveBtn”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_toLeftOf=”@+id/anchor”
        android:layout_marginRight=”10dp”
        android:text=”Save” />
<Button
        android:id=”@+id/cancelBtn”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_toRightOf=”@+id/anchor”
        android:layout_marginLeft=”10dp”
        android:text=”Cancel” />
Try it out (and stop using the WYSIWYG).