Search code examples
androidlayoutwidthandroid-linearlayout

How to make buttons cover full screen width in Android Linear Layout?


I have following three button in a Linear Layout with width fill_parent.

How can I set the width of these buttons to equally cover the whole screen area?

<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnReplyMessage" 
   android:gravity="left"
   android:text="Reply" 
/>
    
<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnMarkAsUnread"
   android:gravity="left" 
   android:text="Mark as unread" 
/>
            
<ImageButton 
   android:id="@+id/btnDeleteMessage"
   android:src="@drawable/imgsearch"
   android:gravity="right" 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_alignParentRight="true"
 />

Solution

  • Give all buttons the following properties

    android:layout_width="fill_parent"
    android:layout_weight="1"
    

    fill_parent tells them to consume as much width as possible, and weight determines how that width shall be distributed, when more than one control are competing for the same space. (Try playing around with different values of weight for each button to see how that works)