E Gomez
E Gomez

Reputation: 77

Can't get Textviews right in layout

This is what I need to achieve:

|TEXTVIEW1|TEXTVIEW2|BUTTON|

TV1 can grow as much as it wants but only taking the space it needs to show the text inside. If the text is bigger than the space it has then it will ellipsize. TV2 has to be always to the right of the text of TV1 and has to show all its content. It cannot have ellipsize. It always has to show its text. TV1 and 2 are single line. BUTTON is fixed at the right of the screen.

Example 1:

TV1 = THIS IS A TEXT WHERE THE TEXT IS TOO LONG TV2 = 1923


| THIS IS A TEXT W...1923 BUTTON|

Example 2:

TV1 = JUST THIS TV2 = 1923


| JUST THIS1923________BUTTON|

(ignore the underscores)

I have tried a thousand different combinations with weights, wrap_content, etc... and still can't get it right.

Help please!

Upvotes: 2

Views: 39

Answers (3)

Sridhar
Sridhar

Reputation: 668

Try this.

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/textview1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_weight="1.0"
    android:text="textview1" />
<TextView
    android:id="@+id/textview2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/textview1"
    android:layout_weight="1.0"
    android:text="textview2" />
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:text="button"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

Upvotes: 0

kerseyd27
kerseyd27

Reputation: 158

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"*
        android:ellipsize"end" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>

This is the sizing you would need to achieve the fitting you desire. After these xml attributes you can add any other styles you wanted to apply like color or text. I starred the single line because this will stop it from growing vertically. If you want the text to be aligned in a row even when the text is smaller than the screen, add android:gravity="right" to tv1 only.

Upvotes: 1

Ashley Alvarado
Ashley Alvarado

Reputation: 1068

I created some little thing for you, I think its what your looking for. The thing you might have not known about is maxLength and singleLine part I believe.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <TextView
            android:layout_width="wrap_content"
            android:maxWidth="100dp"
            android:singleLine="true"
            android:text="I Just this helo this is long"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"/>
</RelativeLayout>

Upvotes: 0

Related Questions