Danil.B
Danil.B

Reputation: 900

Unable to align edge of view to another center aligned view

I have a pretty simple RelativeLayout with following views:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_menu_selector"
    android:minHeight="48dp">

    <ImageView
        android:id="@+id/menuItemAvatar"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="6dp"
        android:background="@color/IconOff" />

    <ImageView
        android:id="@+id/iv_status"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:layout_alignEnd="@id/menuItemAvatar"
        android:layout_alignBottom="@id/menuItemAvatar"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/menuItemAvatar"
        android:gravity="start"
        android:orientation="vertical"
        android:padding="4dp">

        <TextView
            android:id="@+id/menuItemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/TextBlackColor"
            android:textSize="@dimen/main_menu_text_size" />

    </LinearLayout>

</RelativeLayout>

I want the iv_status(the small green square) to be in the right bottom corner of menuItemAvatar, but what I get looks like this: enter image description here

As you can see the bottom edges are not aligned properly. The funny thing is - if I remove the line android:layout_centerVertical="true" from menuItemAvatar it aligns as it should: enter image description here

But then the image is not centered and everything looks shifted in relation to the text.

Upvotes: 1

Views: 34

Answers (1)

omz1990
omz1990

Reputation: 742

I converted your layout to a ConstraintLayout with the right constraints and it seems to be working fine for me. Have a go with this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/main_menu_selector"
    android:minHeight="48dp">

    <ImageView
        android:id="@+id/menuItemAvatar"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="6dp"
        android:background="@color/IconOff"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/iv_status"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:layout_alignEnd="@id/menuItemAvatar"
        android:layout_alignBottom="@id/menuItemAvatar"
        app:layout_constraintEnd_toEndOf="@id/menuItemAvatar"
        app:layout_constraintBottom_toBottomOf="@id/menuItemAvatar"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/menuItemAvatar"
        android:orientation="vertical"
        android:padding="4dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/menuItemAvatar">

        <TextView
            android:id="@+id/menuItemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/TextBlackColor"
            android:textSize="@dimen/main_menu_text_size" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions