3
votes

I am new to android, I have a linear layout list item and would like the TextView itemName to fill the remaining blank space on the row while the other two views (itemIcon and itemTimestamp) just take up as much space as they require hence used wrap_content. I have tried fill_parent and match_parent on ItemName android:layout_width but it ends up hiding itemTimestamp view. I have tried RelativeLayout but had no luck, would prefer to stick with Linearlayout if possible. Appreciate your help.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemNotificationBuzz"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
     android:background="#ffff0000"     
   >

   <LinearLayout 
    android:id="@+id/notificationHeader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

     android:orientation="horizontal" 
     android:background="#ffff0000"
   >

     <ImageView
         android:id="@+id/notificationIcon"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:gravity="left"
         android:maxHeight="35dp"
         android:maxWidth="35dp"
         android:background="#FF000000"
         android:padding="1dp"
         android:src="@drawable/ic_action_place" />

        <TextView
            android:id="@+id/notificationName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp"
            android:textSize="18sp" 
            android:textColor="@android:color/white"
            android:background="#A4C739"
            android:typeface="sans"
            android:textStyle="bold"
            android:maxLines="1"
            android:text="Fname Lname"            
            />

           <TextView
               android:id="@+id/notificationTimestamp"
               android:layout_width = "wrap_content"
                android:layout_height="wrap_content"
               android:gravity="right"

               android:background="#FF000000"
               android:ems="10"
               android:maxLines="1"
               android:text="2m"
               android:textColor="@android:color/white"
               android:typeface="sans" />

           </LinearLayout>

</RelativeLayout>
3

3 Answers

5
votes

If you want to use the LinearLayout you can achieve what you want by using weightSum attribute in your LinearLayout and layout_weight for your item Name TextView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notificationHeader"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffff0000"
    android:orientation="horizontal"
    android:weightSum="1">

    <ImageView
        android:id="@+id/notificationIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF000000"
        android:gravity="left"
        android:maxHeight="35dp"
        android:maxWidth="35dp"
        android:padding="1dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/notificationName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#A4C739"
        android:gravity="center"
        android:maxLines="1"
        android:padding="5dp"
        android:text="Fname Lname"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="sans"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/notificationTimestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF000000"
        android:gravity="right"
        android:maxLines="1"
        android:text="2m"
        android:textColor="@android:color/white"
        android:typeface="sans"/>

</LinearLayout>

This will make your item Name to fill all the space until it reaches the last element. Note: When you use layout_weight you have to set the layout_width to 0dp. And also you should delete ems = 10 from your last element because it makes your notificationTimestamp element to have a larger width and it will push the Name element to the left leaving an empty space.

0
votes

You can directly use RelativeLayout to achieve what you want. The answer is already here at How to make layout with View fill the remaining space?

If you want to still use the LinearLayout then that is also possible. In this case to achieve what you want, set the layout_weight property of the widgets that you want to just wrap content with to 0 (you should still set them to wrap_content) and then set the layout_weight of the widget that you want to occupy the remaining space to 1.

0
votes

There is no reason to use RelativeLayout here. What you need to use is layout-weight. You need to set layout_width to 0dp and layout-weight to 1

Here is the updated layout (btw, I removed the parent RelativeLayout as it didn't have any use):

<ImageView
    android:id="@+id/notificationIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:maxHeight="35dp"
    android:maxWidth="35dp"
    android:background="#FF000000"
    android:padding="1dp"
    android:src="@drawable/ic_action_place" />

<TextView
    android:id="@+id/notificationName"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:textSize="18sp"
    android:textColor="@android:color/white"
    android:background="#A4C739"
    android:typeface="sans"
    android:textStyle="bold"
    android:maxLines="1"
    android:text="Fname Lname"
    />

<TextView
    android:id="@+id/notificationTimestamp"
    android:layout_width = "wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"

    android:background="#FF000000"
    android:ems="10"
    android:maxLines="1"
    android:text="2m"
    android:textColor="@android:color/white"
    android:typeface="sans" />