Centering Textview Below a image button that is under relative layout?

put the button and text view in the same vertical LinearLayout (and you can place the LinearLayout in a relative layout) and simply assign center gravity to the text view.


Try this snippet :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center_vertical" >

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

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>

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

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>

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

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>
</LinearLayout>

enter image description here


Just adding these values:

 <TextView
    android:layout_alignLeft="@+id/my_image_view"
    android:layout_alignRight="@+id/my_image_view"
    android:gravity="center" />

You can do, simply (!!!):

android:layout_alignRight="@id/my_image_view"
android:layout_alignLeft="@id/my_image_view"

just align it left and right together, the result is the TextView centered.