kumar
kumar

Reputation: 1854

Android Relative layout child with alignParentEnd and toEndOf

Can we combine both layout_alignParentEnd and layout_toEndOf in relative layout to align an item?

In the required output a textView should be aligned to right end of parent and when it grows it should not overlap on its previous element instead it should end with ellipsize(as shown in expected_result_2)

I have the following snippet where it produces the expected_result_2 but not the expected_result_1. Instead of aligning at the end of parent, it aligns next to second_text as shown in actual_result_1 .

<RelativeLayout
    android:id="@+id/row_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/indicator"
        style="@style/material_icon_style"
        android:layout_width="20dp"
        android:layout_height="24dp"
        android:layout_alignParentStart="true"
        android:scaleType="fitXY"
        android:src="@drawable/ic_attachment"
        android:visibility="invisible"
        tools:visibility="visible"/>

    <TextView
        android:id="@+id/first_text"
        style="@style/list_primary_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="false"
        android:layout_toEndOf="@+id/indicator"
        android:singleLine="true"
        tools:text="Jul 4, 2017"/>

    <TextView
        android:id="@+id/second_text"
        style="@style/list_primary_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@id/first_text"
        android:singleLine="true"
        android:text="@string/note_draft"
        android:textColor="@color/highlight"
        android:visibility="gone"
        tools:text="DRAFT"
        tools:visibility="visible"/>

    <TextView
        android:id="@+id/last_item"
        style="@style/list_secondary_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/second_text"
        android:layout_alignBottom="@id/second_text"
        android:singleLine="true"
        android:visibility="invisible"
        android:ellipsize="end"
        android:paddingStart="10dp"
        tools:text="RIGHT"
        tools:visibility="visible"/>

</RelativeLayout>

actual_result_1 : actual_result_1

expected_result_1 : expected_result_1

expected_result_2 : expected_result_2

Should we use constraintlayout to get the desired result?

Upvotes: 0

Views: 634

Answers (2)

SpiritCrusher
SpiritCrusher

Reputation: 21043

Try the layout below .

 <RelativeLayout
    android:id="@+id/row_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/indicator"
        android:layout_width="20dp"
        android:layout_height="24dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"
        android:visibility="visible"
        tools:visibility="visible"/>

    <TextView
        android:id="@+id/first_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/indicator"
        android:singleLine="true"
        android:text="Jul 4, 2017"/>

    <TextView
        android:id="@+id/second_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/first_text"
        android:singleLine="true"
        android:text="DRAFT"
        android:layout_marginLeft="10dp"
        tools:text="DRAFT"
        tools:visibility="visible"/>

    <TextView
        android:id="@+id/last_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/second_text"
        android:singleLine="true"
        android:ellipsize="end"
        android:gravity="end"
        android:layout_toRightOf="@+id/second_text"
        android:text="ksljklsjkljl"
        android:layout_alignParentRight="true"
        tools:text="RIGHT"
        tools:visibility="visible"
        />

</RelativeLayout>

Upvotes: 2

Navneet Krishna
Navneet Krishna

Reputation: 5017

use android:gravity="end" in textview last_item

Upvotes: 1

Related Questions