I always read about this funny weight value in the Android documentations. Now I want to try it for the first time but it isn't working at all.
As I understand it from the documentations this layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
</LinearLayout>
should create two buttons that are horizontally aligned and share the space equally. The problem is the two buttons don't grow to fill the space.
I would like the buttons to grow and fill the whole line. If both buttons are set to match parent only the first button is shown and fills the whole line.
Substitute
wrap_content
withfill_parent
.Plus you need to add this
android:layout_width="0dp"
for children views [Button views] ofLinerLayout
You have to write like this its Working for me
Below are the changes (Marked in BOLD) in your code:
Since your LinearLayout has orientation as horizontal, therefore you will need to keep your width only as 0dp. for using weights in that direction . (If your orientation was vertical, you would have kept your height only 0dp).
Since there are 2 views and you have placed
android:layout_weight="1"
for both the views, it means it will divide the two views equally in horizontal direction (or by width).3 things to remember:
Example:
And the result: