2

There are two buttons :

<LinearLayout 
    android:layoutWidth="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

   <Button 
       android:id="@+id/btn1"
       android:layoutWidth="wrap_content"
       android:layout_height="wrap_content"
       android:text="Launch camera"
       android:onClick="launchCamera" />

   <Button 
       android:id="@+id/btn2"
       android:layoutWidth="wrap_content"
       android:layout_height="wrap_content"
       android:text="List of photos"
       android:onClick="listPhotos" />

</linearLayout>

How to make these two buttons having equal size and spanning all their parent's width ?

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
pheromix
  • 18,213
  • 29
  • 88
  • 158

5 Answers5

4

By using "weightSum" attribute at LinearLayout and "layout_weight" and "width" = zero at its children (Buttons) and this Link for explaining What is android:weightSum and how it works in Android

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:gravity="center">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Launch camera"
        android:onClick="launchCamera" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="List of photos"
        android:onClick="listPhotos" />
</LinearLayout>
Community
  • 1
  • 1
Ayman Mahgoub
  • 4,152
  • 1
  • 30
  • 27
3

Answer:

Very important, both buttons are aligned side by side(android:orientation="horizontal") and notice that there is a android:layout_weight="1".

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
  <Button
        android:id="@+id/btn1"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Launch camera"
        android:onClick="launchCamera" />
  <Button
        android:id="@+id/btn2"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="List of photos"
        android:onClick="listPhotos" />
</LinearLayout>

Please see other examples here: Linear Layout and weight in Android

Example of the weights, using just layouts and colors:

enter image description here

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
3

Use weights:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
   <Button android:id="@+id/btn1"
       android:layout_width="0dp"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       android:text="Launch camera"
       android:onClick="launchCamera" />
   <Button android:id="@+id/btn2"
       android:layout_width="0dp"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       android:text="List of photos"
       android:onClick="listPhotos" />
</LinearLayout>
computingfreak
  • 4,939
  • 1
  • 34
  • 51
moictab
  • 959
  • 6
  • 27
2

You can use android:weightSum on the LinearLayout and android:layout_weight on your Buttons.

<LinearLayout android:layoutWidth="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:gravity="center">
   <Button android:id="@+id/btn1"
       android:layoutWidth="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="Launch camera"
       android:onClick="launchCamera" />
   <Button android:id="@+id/btn2"
       android:layoutWidth="match_parent"
       android:layout_weight="1"
       android:layout_height="wrap_content"
       android:text="List of photos"
       android:onClick="listPhotos" />
</LinearLayout>
Ralf
  • 709
  • 6
  • 14
0

By using LinearLayout Weight

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
    android:id="@+id/btn1"
    android:text="Launch camera"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_weight="0.5"
    android:onClick="launchCamera" />
 <Button
    android:id="@+id/btn2"
    android:text="List of photos"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_weight="0.5"  />    
</LinearLayout>

The default weight sum is 1.0, so 0.5 is enough in this example of 2 children Views.

shkschneider
  • 17,833
  • 13
  • 59
  • 112