Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - placing booth fixed size and dynamic size view in row

Tags:

android

I have a dynamic size RelativeLayout containing TextView with a arbitrary text. Also i have fixed size TextView with timestamp. I want to place both RelativeLayout and TextView with timestamp in one line, side by side.

Like this:

What I want to get

What I'm really getting:

What I'm really getting

As you can see, layout with text just occupies the entire width.

XML:

    <RelativeLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/news_list_element"
                android:layout_marginRight="5dip"
                android:layout_alignParentLeft="true"
                android:focusable="false"
                android:paddingRight="8dip"
                android:paddingLeft="17dip"
                android:paddingTop="4dip"
                android:paddingBottom="4dip">
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/news_list_text"
              android:background="@android:color/transparent"
              android:textColor="#000000"
              android:focusable="false"
              android:padding="3dip"
            />
</RelativeLayout>
<TextView android:id="@+id/messages_dialog_timestamp_in"
          android:background="#000000"
          android:text="12:05"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_toRightOf="@id/news_list_element"
          android:paddingBottom="9dip"
          android:paddingLeft="0dip"
          android:textColor="#8f9eac"
        />
like image 282
user1003208 Avatar asked Dec 05 '25 10:12

user1003208


1 Answers

Try this:

<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:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:id="@+id/news_list_text" 
        android:background="@android:color/transparent" 
        android:textColor="#000000" 
        android:focusable="false" 
        android:padding="3dip" 
        android:layout_weight="1"
        android:layout_gravity="left"
    /> 
    <TextView android:id="@+id/messages_dialog_timestamp_in" 
        android:background="#000000" 
        android:text="12:05" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingBottom="9dip" 
        android:paddingLeft="0dip" 
        android:textColor="#8f9eac" 
        android:layout_gravity="right"
    /> 

The android:layout_gravity="left" will make the message TextView start from the left edge of the parent.

The android:layout_gravity="right" will put the timestamp Textview on the right edge of the parent.

The android:layout_width="0dp" combined with the android:layout_weight="1" in the message TextView will cause it to expand to fill all remaining space after all other widgets are in place and have their required space set aside (so it won't push the timestamp off the screen).

This layout will be a little different from what you show as even with short messages the Textview area will expand to fill the available space and make the timestamps all appear in a vertical line on the right edge (which would look neater IMO).

like image 132
Barak Avatar answered Dec 07 '25 22:12

Barak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!