Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android LinearLayout View item in xml does not fill empty space

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>
like image 592
DeveloperDH Avatar asked Dec 05 '25 08:12

DeveloperDH


1 Answers

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.

like image 200
Laura Avatar answered Dec 07 '25 22:12

Laura



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!