Search code examples
androidtextviewimageviewcentergravity

How can I center my textView in the middle of my ImageView?


I want to center vertical and horizontal my textView. Now I have the textView in the left corner of my imageView. Thanks a lot

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="70dp"
    android:layout_weight="3" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:src="@drawable/numero" />

    <TextView
        android:id="@+id/list_item_numero"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="15sp"
        android:gravity="center" />
</RelativeLayout>

Solution

  • Using a RelativeLayout it's easy!

    <TextView
            android:id="@+id/list_item_numero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textStyle="bold"
            android:textSize="15sp"
            android:gravity="center" />
    

    By the way, it's not necessary to make use of android:layout_weight when using RelativeLayout.