Search code examples
androidtextviewalignmentandroid-linearlayout

Layout 2 TextViews with different "priorities"


I have this

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:ellipsize="end" />

    <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="right"
            android:maxLines="1" />
</LinearLayout>

But i want text2 to have "higher priority" than text1

Meaning In the case where the screen doesn't fit both i need to have this behavior:

Text2 must always be fully displayed in a single line unless it's over 80% of the screen, in that case use "..."

Text1 uses whatever space is left from Text2, can use multiple lines, if it doesn't fit, it should use the "..." at the end

Is this possible with LinearLayout or maybe i should use a different container instead?


Solution

  • Consider using ConstraintLayout

    https://developer.android.com/training/constraint-layout/index.html