Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android -- Layout ellipsize with right-aligned view

Tags:

android

How can I achieve this as Text1 gets longer?

|[Text1] [Text2]_____________|

|[Text1 Text1 Text1] [Text2]____|

|[Text1 Text1 Text1 Tex...][Text2]|

Text2 should always be on the right of Text1, but when Text1 is too large, it is ellipsized and Text2 is right-aligned.

like image 845
Chris Allen Avatar asked Oct 06 '11 04:10

Chris Allen


3 Answers

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="aaaaaaaaaaaaaaaaaaaaaaaa"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="bbbbb"
        android:textSize="30dp"/>
</LinearLayout>
like image 85
Ilan Klinghofer Avatar answered Oct 31 '22 17:10

Ilan Klinghofer


I had a scenario where I needed to have it like this (pretty similar to the OP)

[[TextView1][Icon]_______[TextView2]]

[[TextView1 long..][Icon][TextView2]]

So the TextView1 can have arbitrary length and should be ellipsized if it can't fit any more, but the Icon should be right next to it and the TextView2 should always be right-alighned

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/TextView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:ellipsize="end"
                        .... />

                    <ImageView
                        android:id="@+id/Icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        .... />

                </LinearLayout>

            </FrameLayout>

            <TextView
                android:id="@+id/TextView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                .... />
        </LinearLayout>
like image 28
user1071762 Avatar answered Oct 31 '22 17:10

user1071762


You should use RelativeLayout Like this ,

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:text="@string/radio_group_1"
    android:ellipsize="end" android:singleLine="true"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/txt2"/>
<TextView android:text="@string/Pink_Floyd" android:id="@+id/txt2"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />

</RelativeLayout>
like image 21
Herry Avatar answered Oct 31 '22 16:10

Herry