Flomp
Flomp

Reputation: 524

center text and image in RelativeLayout

I am coming to you with a seemingly simple question. Yet I was not able to find a suitable solution. I have an Image- and a TextView inside a RelativeLayout inside a LinearLayout.

<LinearLayout
    android:id="@+id/page2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="horizontal"
    android:weightSum="2"
    android:baselineAligned="false"
    android:divider="@drawable/separator_vertical"
    android:showDividers="middle">

    <RelativeLayout
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">


        <TextView
            android:id="@+id/delete_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="DemoText"
            android:textColor="#fff"
            android:textSize="15sp" />

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@id/delete_text"
            android:src="@drawable/ic_delete" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/edit_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextView
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="DemoText"
            android:textColor="#fff"
            android:textSize="15sp" />

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@id/edit_text"
            android:src="@drawable/ic_edit" />

    </RelativeLayout>


</LinearLayout>

enter image description here

Currently only the TextView is centered in their respective RelativeLayout. How can I align the ImageView and the TextView together in the center of the RelativeLayout?

Upvotes: 1

Views: 1735

Answers (3)

muminers
muminers

Reputation: 1210

LinearLayout has different characteristic, what you need is ConstraintLayout and LinearLayout inside. Of course you can wrap RelativeLayout with something else but it is unnecessary nesting:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:baselineAligned="false"
android:orientation="horizontal"
android:showDividers="middle"
android:weightSum="2">

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/delete_button"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/guideline">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_toStartOf="@id/delete_text"
        android:src="@drawable/delete_text" />

    <TextView
        android:id="@+id/delete_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DemoText"
        android:textColor="#fff"
        android:textSize="15sp" />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/edit_button"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/guideline">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_toStartOf="@id/edit_text"
        android:src="@drawable/edit_text" />

    <TextView
        android:id="@+id/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DemoText"
        android:textColor="#fff"
        android:textSize="15sp" />

</LinearLayout>

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Santanu Sur
Santanu Sur

Reputation: 11477

Set

android:gravity="center"

to the parent relativeLayouts having weights.

So that it centers its children, as you wanted !!

Upvotes: 2

ahomphophone
ahomphophone

Reputation: 319

You can wrap the two views in another layout and center it. For example:

<RelativeLayout
    android:id="@+id/delete_button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginEnd="10dp"
            android:src="@drawable/ic_delete" />

        <TextView
            android:id="@+id/delete_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DemoText"
            android:textColor="#fff"
            android:textSize="15sp" />

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Related Questions