TextView with different text lengths aligning next to horizontally

950 views Asked by At

I'm looking for efficient way (best XML only) to align two TextViews horizontally, smth like this:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/opinion_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"/>
        <TextView
            android:id="@+id/opinion_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:maxLines="1"/>
    </LinearLayout>

opinion_date will always have ~50-70dp length and must be fully visible aligned to right of opinion_author. author may have 0-255 chars and if too much for screen then ellipsize then.

In above when author is too long view is pushing out date view. Same situation when using toRightOf/toLeftOf when using RelativeLayout. Setting layout_weight=1 for author keeping date sticked to right edge of parent viewgroup even when author have only few characters, so thre is a lot of space... I'm looking for a way, which aligns date to right of author even when it is ellipsized and ends with... I have few ideas how to solve it in Java (measuring views or textpaint, but all seems not so efficient...)

4

There are 4 answers

1
Radek Kutyłowski On BEST ANSWER

Try this code:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:singleLine="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:ellipsize="none"
        android:gravity="center"
        android:lines="1"
        android:singleLine="true" />
</TableRow>

5
Dhalloo On

Give android:weightSum = "1" to your linear layout. Give android:maxWidth="250dp" to opinion_author textview. Give android:layout_weight="0.3" to opinion_date textview.

Something like given below.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/opinion_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxWidth="250dp"
            android:lines="1"
            android:maxLines="1"/>
        <TextView
            android:id="@+id/opinion_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:layout_weight="0.3"
            android:maxLines="1"/>
    </LinearLayout>
1
TWL On

use weights!

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">
        <TextView
            android:id="@+id/opinion_author"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:layout_weight="2"/>
        <TextView
            android:id="@+id/opinion_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:lines="1"
            android:maxLines="1"
            android:layout_weight="1"/>
    </LinearLayout>

example:

android:weightSum="3" divies up into 3 parts

android:layout_weight="2" gives 2 parts to author

android:layout_weight="1" gives 1 part to date

play with the ratios till you're pleased!

(oh, android:layout_width="0dp" because layout_weights overrides them)

3
yasser karimi On

set size in android:layout_width

like:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/opinion_author"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"/>
    <TextView
        android:id="@+id/opinion_date"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:lines="1"
        android:maxLines="1"/>
</LinearLayout>

you can use android:layout_weight in LinearLayout child.