Search code examples
androidxmlandroid-relativelayout

How to center align multiple items in same row within relative layout


Is it possible to center align these two items horizontally together without using 1 table row? These items are within a relative layout.

    <ImageView
        android:id="@+id/image_warning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/stat_sys_warning"
        android:padding="5dp" />
    <TextView
        android:id="@+id/textView_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="message"
        android:layout_toEndOf="@id/image_warning" />

Solution

  • You can use "drawableLeft" and remove the imageView.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:background="@android:color/holo_orange_light"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableLeft="@android:drawable/stat_sys_warning"
            android:gravity="center"
            android:text="message" />
    </RelativeLayout>