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
Like answer of @Manoj Seelan
Replace android:layout_weight
With android:weight
.
When you use Weight with LinearLayout
. you must add weightSum
in LinearLayout
and according to orientation of your LinearLayout
you must setting 0dp
for Width/Height to all LinearLayout
`s Children views
Example :
If The orientation of Linearlayout
is Vertical
, then Set Width of all LinearLayout
`s Children views with 0dp
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="3">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="2" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
If the orientation Linearlayout
of is horizontal
, then Set Height of all LinearLayout
`s Children views with 0dp
.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="10dip"
android:layout_weight="2" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="10dip"
android:layout_weight="1" />
</LinearLayout>
Plus you need to add this android:layout_width="0dp"
for children views [Button views] of LinerLayout
It's android:layout_weight
. Weight can only be used in LinearLayout
. If the orientation of linearlayout is Vertical, then use android:layout_height="0dp"
and if the orientation is horizontal, then use android:layout_width = "0dp"
. It'll work perfectly.
In the above XML, set the android:layout_weight
of the linear layout as 2
:
android:layout_weight="2"
3 things to remember:
Example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
And the result:
You are not setting the layout_weight
property. Your code reads weight="1"
and it should read android:layout_weight="1"
.