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

40,808

Solution 1

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)

Solution 2

You should just specify these attributes for each button:

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

So it should be something like that:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

Solution 3

you can use following code:

  <Button
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"/>

You must to do 0dp in width on every button.

Share:
40,808
UMAR-MOBITSOLUTIONS
Author by

UMAR-MOBITSOLUTIONS

I am associated with A UK Based Software Development Company! - Web Design and Development Company in UK Our Company provides: Website Development | Mobile Apps Development | Games Development | Ui and UX | Consultancy | API | Digital Marketing My Expertise in: Asp.net (1.1,2.0,3.5,4.0,4.5) Android programming. PHP,WordPress. android,iPhone application development. Unity games development Project management Software quality assurance &amp; testing. Digital Marketing

Updated on July 22, 2022

Comments

  • UMAR-MOBITSOLUTIONS
    UMAR-MOBITSOLUTIONS almost 2 years

    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"
     />