wbk727
wbk727

Reputation: 8408

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" />

Upvotes: 0

Views: 509

Answers (2)

Dus
Dus

Reputation: 4240

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>

Upvotes: 0

Angelo Parente
Angelo Parente

Reputation: 818

What if you put the two items in a linearLayout and then set the gravity center horizontal?

Upvotes: 1

Related Questions