Reputation: 19287
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 ?
Upvotes: 2
Views: 1014
Reputation: 959
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>
Upvotes: 3
Reputation: 55517
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
Upvotes: 3
Reputation: 4210
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>
Upvotes: 4
Reputation: 18243
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.
Upvotes: 0
Reputation: 719
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>
Upvotes: 2