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.
It's
android:layout_weight
. Weight can only be used inLinearLayout
. If the orientation of linearlayout is Vertical, then useandroid:layout_height="0dp"
and if the orientation is horizontal, then useandroid:layout_width = "0dp"
. It'll work perfectly.This image summarizes the Linear layout.
You can follow this link for more information on the topic. Just Maths - Views, View Groups and Layouts
Video Tutorial For Linear Layout : Width, Height & Weights
Android Linear Layout Tutorial
Perhaps setting both of the buttons layout_width properties to "fill_parent" will do the trick.
I just tested this code and it works in the emulator:
Be sure to set layout_width to "fill_parent" on both buttons.
This is perfect answer of your problem
Like answer of @Manoj Seelan
Replace
android:layout_weight
Withandroid:weight
.When you use Weight with
LinearLayout
. you must addweightSum
inLinearLayout
and according to orientation of yourLinearLayout
you must setting0dp
for Width/Height to allLinearLayout
`s Children viewsExample :
If The orientation of
Linearlayout
isVertical
, then Set Width of allLinearLayout
`s Children views with0dp
If the orientation
Linearlayout
of ishorizontal
, then Set Height of allLinearLayout
`s Children views with0dp
.In the above XML, set the
android:layout_weight
of the linear layout as2
:android:layout_weight="2"