Q:

WeightSum not working as expected in a LinearLayout in android

Well, I have a LinearLayout with two children, an EditText and a Button and they both have a weightSum of 3. All I want is to have a horizontal line with 2 layout_weight EditText and 1 layout_weight Button. Here is my code:
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3">

    <EditText
        android:id="@+id/edit1"
        android:inputType="textAutoComplete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/DBLocations"
        android:layout_weight="1"/>
</LinearLayout>

But weights aren't working, here is the result:
screenshot of the wrong result
What I have to do in order to have 2 layout_weight in EditText and 1 for the Button?

A:

You should set android:orientation="horizontal" in your XML code .
And add android:layout_width="0dp" in your Button and EditText .
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal
    android:weightSum="3">

<EditText
    android:id="@+id/edit1"
    android:inputType="textAutoComplete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/DBLocations"
    android:layout_weight="1"/>
</LinearLayout>

